title: Null pointer Exception - findViewById()
body: <p>Can anyone help me to find out what can be the issue with this program.
  In the <code>onCreate()</code> method the <code>findViewById()</code> returns null for all ids and this causes a null pointer exception later. I can not figure out why the <code>findViewById()</code> can not find the view. Any suggestions?</p>
  
  <p>This is the main code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      ViewPager pager;
      MyPagerAdapter adapter;
      LinearLayout layout1, layout2, layout3;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          layout1 = (LinearLayout) findViewById(R.id.first_View);
          layout2 = (LinearLayout) findViewById(R.id.second_View);
          layout3 = (LinearLayout) findViewById(R.id.third_View);
  
          adapter = new MyPagerAdapter();
          pager = (ViewPager) findViewById(R.id.main_pager);
          pager.setAdapter(adapter);
      }
  
      private class MyPagerAdapter extends PagerAdapter
      {
  
          @Override
          public int getCount() { 
              return 3;
          }
  
          @Override
          public Object instantiateItem(ViewGroup collection, int position) {
  
              LinearLayout l = null;
  
              if (position == 0 )
              {
                  l = layout1;
              }
              if (position == 1)
              {
                  l = layout2;
              }
  
              if (position == 2)
              {
                  l = layout3;
              }
                  collection.addView(l, position);
                  return l;
          }
  
          @Override
          public boolean isViewFromObject(View view, Object object) {
              return (view==object);
          }
  
           @Override
           public void destroyItem(ViewGroup collection, int position, Object view) {
                   collection.removeView((View) view);
           }
      }
  }
  </code></pre>
  
  <p>And the related XML files:</p>
  
  <p>activity_main layout: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="#a4c639"&gt;
  
  
      &lt;android.support.v4.view.ViewPager
                          android:layout_width="match_parent" 
                          android:layout_height="match_parent" 
                          android:id="@+id/main_pager"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>activity_first layout:</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:id="@+id/first_View"&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hello_world" /&gt;
  
  &lt;Button
      android:id="@+id/button1"
      style="?android:attr/buttonStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>activity_second layout:</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:id="@+id/second_View"&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hello_world" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And the activity_third layout: </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:id="@+id/third_View"&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hello_world" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>