title: How do I add a LinearLayoutCompat view to an existing xml layout dynamically?
body: <p>I have a database containing of Texts.My activity has a TextView at the top(this should be constantly intact throughout) and below that I have a RelativeLayout which will be my container. In my code, as soon as the activity (in which I will be inserting view dynamically) launches, I create a LinearLayoutCompact object and set some properties. Then I process the cursor to the table data and I create a TextView for each database element and add it to the LinearLayoutCompat object. Finally I try to add this object into the RelativeLayout container where the app crashes. I am using LayoutInflater and treating the RelativeLayout container as a ViewGroup (I found this way after doing some research). However, if I simply use setContentView on the LinearLayoutCompat object it works fine but also replaces that TextView I want to be present. Here are the files I think will be necessary :</p>
  
  <p><strong>veg_option.xml</strong>  (layout of my activity)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;TextView             &lt;!-- The one I want intact throughout --&gt;
          android:id="@+id/narrowText"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Let's narrow down now !"
          android:textSize="20sp"
          android:textColor="@color/wineBrown"
          android:gravity="center"
          android:padding="10dp"
          android:layout_marginTop="30dp"
          android:elevation="4dp"/&gt;
     &lt;RelativeLayout               &lt;!-- Trying to use this as container --&gt;
          android:id="@+id/dataCont"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center"&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>VegOptions.java</strong>   (The activity where all action is supposed to happen)</p>
  
  <pre><code>package com.techpappy.whattoeat;
  
  /**
   * Created by Mehul on 31-05-2017.
   */
  
  import android.content.Context;
  import android.database.Cursor;
  import android.graphics.Color;
  import android.graphics.Typeface;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.LinearLayoutCompat;
  import android.support.v7.widget.AppCompatTextView;
  import android.util.TypedValue;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.os.Bundle;
  import android.view.ViewGroup;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.w3c.dom.Text;
  
  import java.util.ArrayList;
  
  public class VegOptions extends AppCompatActivity{
  
      protected DBSource vegDBSource;
      private Cursor cursor;
      protected ArrayList&lt;String&gt; mTypes;
      LayoutInflater li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View v = li.inflate(R.layout.veg_option, null);
      ViewGroup dataConPt = (ViewGroup) findViewById(R.id.dataCont);
  
      @Override
      public void onCreate(Bundle bundle) {
          super.onCreate(bundle);
          setContentView(R.layout.veg_option);   
          vegDBSource = new DBSource(VegOptions.this);
          disp();
      }
  
      private void disp()
      {
          vegDBSource.open();
          cursor = vegDBSource.getInfo();
          LinearLayoutCompat linearLayoutCompat = defaultPage(cursor);
         //setContentView(linearLayoutCompat);  Works fine but replaces all
          dataConPt.addView(linearLayoutCompat);
      }
  //Here my LinearLayoutCompat object is prepared and returned
      public LinearLayoutCompat defaultPage(Cursor mCursor) {
          LinearLayoutCompat layout = new LinearLayoutCompat(this);
          LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT);
          layout.setLayoutParams(params);
          layout.setGravity(Gravity.CENTER);
          layout.setOrientation(LinearLayoutCompat.VERTICAL);
          mCursor.moveToFirst();  //Each iteration adds one TextView
          do {
              AppCompatTextView defaultText = new AppCompatTextView(this);
              defaultText.setText(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.COLUMN_TYPES)));
              defaultText.setTextAppearance(getApplicationContext(), R.style.TextFontMain);
              defaultText.setTextSize(30);
              defaultText.setPadding(0,30,0,30);
              final String temp = (String) defaultText.getText();
              defaultText.setGravity(Gravity.CENTER);
              defaultText.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View arg0) {
                       Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_SHORT).show();
                  }
              });
              layout.addView(defaultText);
              mCursor.moveToNext();
          }  while(mCursor.isAfterLast() == false);
          return layout;  //LinearLayoutCompat containing all Texts returned
      }
  
      @Override
      protected void onResume() {
          super.onResume();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          vegDBSource.close();
          overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
      }
  }
  </code></pre>
  
  <p>My previous questions have been downvoted but this time I have done a fair amount of research and found the correct way but I can't get it to work in my unique case</p>
  
  <p><strong>stacktrace</strong></p>
  
  <pre><code>6-05 19:32:17.679 3450-3450/com.techpappy.whattoeat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.techpappy.whattoeat, PID: 3450
                                                                         java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.techpappy.whattoeat/com.techpappy.whattoeat.VegOptions}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                             at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
                                                                             at com.techpappy.whattoeat.VegOptions.&lt;init&gt;(VegOptions.java:33)
                                                                             at java.lang.Class.newInstance(Native Method)
                                                                             at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
  </code></pre>