title: Get linearLayout from another xml Android
body: <p>In need to get a LinearLayout that isn't located in the main xml file (the one that i set in setContentView()). So in write this code:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      LinearLayout main = (LinearLayout) findViewById(R.id.main1);
  
      LinearLayout oggetto = (LinearLayout) findViewById(R.id.element1);
  
      main.addView(oggetto);
  }
  </code></pre>
  
  <p>The xml file where element1 is written is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
          &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/element1"
              android:layout_width="fill_parent"
              android:orientation="horizontal" 
              android:layout_height="90px"
              android:background="#000000"&gt;
  
              &lt;LinearLayout
                  android:id="@+id/linearLayoutLeftBar"
                  android:layout_width="10px"
                  android:layout_height="fill_parent"
                  android:orientation="horizontal"
                  android:background="#7FFF00" &gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/linearLayoutRightContent"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  android:background="#EEEEEE" &gt;
  
                  &lt;LinearLayout
                      android:id="@+id/linearLayout1"
                      android:layout_width="fill_parent"
                      android:layout_height="45px"
                      android:orientation="horizontal"
                      android:background="#EEEEEE" &gt;
  
                      &lt;LinearLayout
                          android:id="@+id/linearLayoutTxtView1"
                          android:layout_width="wrap_content"
                          android:layout_height="fill_parent"
                          android:orientation="vertical"
                          android:background="#EEEEEE"
                          android:layout_gravity="right" &gt;
  
                          &lt;TextView
                              android:id="@+id/textView1"
                              android:layout_width="wrap_content"
                              android:layout_height="fill_parent"
                              android:text="TextBox1"
                              android:layout_marginLeft="5px"
                              android:textColor="#000000"
                              android:gravity="center" /&gt;
  
                      &lt;/LinearLayout&gt;
  
                      &lt;LinearLayout
                          android:id="@+id/linearLayoutImgView"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:orientation="vertical"
                          android:gravity="right|center" &gt;
  
                          &lt;ImageView
                              android:id="@+id/imageView1"
                              android:layout_width="16px"
                              android:layout_height="16px"
                              android:layout_marginRight="16px"
                              android:src="@drawable/ic_launcher"
                              android:background="#FF0000"/&gt;
  
                      &lt;/LinearLayout&gt;
  
                  &lt;/LinearLayout&gt;
  
                  &lt;LinearLayout
                      android:id="@+id/linearLayout2"
                      android:layout_width="fill_parent"
                      android:layout_height="45px"
                      android:orientation="horizontal"
                      android:background="#EEEEEE" &gt;
  
                      &lt;LinearLayout
                          android:id="@+id/linearLayoutTxtView1"
                          android:layout_width="wrap_content"
                          android:layout_height="fill_parent"
                          android:orientation="vertical"
                          android:background="#EEEEEE"
                          android:layout_gravity="right" &gt;
  
                          &lt;TextView
                              android:id="@+id/textView2"
                              android:layout_width="wrap_content"
                              android:layout_height="fill_parent"
                              android:text="TextBox2"
                              android:layout_marginLeft="5px"
                              android:textColor="#000000"
                              android:gravity="center" /&gt;
  
                      &lt;/LinearLayout&gt;
  
                      &lt;LinearLayout
                          android:id="@+id/linearLayoutImgView"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"
                          android:orientation="vertical"
                          android:gravity="right|center" &gt;
  
                          &lt;TextView
                              android:id="@+id/textView3"
                              android:layout_width="wrap_content"
                              android:layout_height="fill_parent"
                              android:layout_marginRight="16px"
                              android:text="TextBox3"
                              android:textColor="#000000"
                              android:gravity="center|right" /&gt;
  
                      &lt;/LinearLayout&gt;
  
                  &lt;/LinearLayout&gt;
  
              &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The problem is that I get an error when I start the app: Unable to start activity ComponentInfo{com.chiamata/com.chiamata.ChiamataActivity}: java.lang.NullPointerException 
  This because element1 is null. If I put if(element1 != null) before adding it to main Linearlayout everything works fine, but element1 is obviously not added. I need to add it programmatically, so I can't put all in one xml file.
  How can I do?
  Thanks, Mattia</p>