title: NullpointerException using ViewPager
body: <p>I want to have Tabs in my Application and to do so I use the <code>ViewPager</code> but I get this logcat: </p>
  
  <pre><code>04-17 22:03:31.291  10545-10545/de.gestureanywhere E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: de.gestureanywhere, PID: 10545
  java.lang.NullPointerException
          at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:416)
          at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:411)
          at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
          at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
          at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
          at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
          at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
          at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
          at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
          at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
          at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2556)
          at android.view.View.measure(View.java:16514)
          at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
          at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
          at android.view.Choreographer.doCallbacks(Choreographer.java:574)
          at android.view.Choreographer.doFrame(Choreographer.java:544)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
          at android.os.Handler.handleCallback(Handler.java:733)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5034)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Here is my code: </p>
  
  <pre><code>    public class Vorschlag extends ActionBarActivity implements
          ActionBar.TabListener {
  
      private ViewPager viewPager;
      private TabsAdapter mAdapter;
      private ActionBar actionBar;
  
      // Tab titles
      private String[] tabs = { "Neuer Vorschlag", "Vorschläge" };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          requestWindowFeature(Window.FEATURE_ACTION_BAR);
          super.onCreate(savedInstanceState);
  
  
  
          setContentView(R.layout.vorschlag_layout);
  
          ActionBar actionBar = getSupportActionBar();
  
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  
          actionBar.setDisplayShowTitleEnabled(true);
  
          mAdapter = new TabsAdapter(getSupportFragmentManager());
  
          // Initilization
          viewPager = (ViewPager) findViewById(R.id.pager);
  
         // actionBar = getApplicationContext().getS
  
  
          viewPager.setAdapter(mAdapter);
  
  
  
          // Adding Tabs
          for (String tab_name : tabs) {
              actionBar.addTab(actionBar.newTab().setText(tab_name)
                      .setTabListener(this));
          }
      }
  
  
  
  
  
      @Override
      public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
  
      }
  
      @Override
      public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
  
      }
  
      @Override
      public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
  
      }
  }
  </code></pre>
  
  <p>this is the layout file: </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="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.v4.view.ViewPager
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
      &lt;/android.support.v4.view.ViewPager&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And here is my Adapter class: </p>
  
  <pre><code>import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  
  
  /**
   * Created by Viktor on 17.04.2015.
   */
  public class TabsAdapter extends FragmentPagerAdapter {
  
      public TabsAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public android.support.v4.app.Fragment getItem(int index) {
  
          switch (index) {
              case 0:
                  // Top Rated fragment activity
                  // return new TopRatedFragment();
                  break;
              case 1:
                  // Games fragment activity
                  // return new GamesFragment();
                  new VorhandeneVorschlaege();
                  break;
  
          }
  
          return null;
      }
  
      @Override
      public int getCount() {
          // get item count - equal to number of tabs
          return 2;
      }
  
  }
  </code></pre>
  
  <p>The Tabs are now displayed thanks. But I have got the problem, that the Tabs are only displayed, when <code>getCount()</code> returns 0. When I return the number of tabs there, the app is terminating. 
  Here is the code: </p>
  
  <p>public class TabsAdapter extends FragmentPagerAdapter {</p>
  
  <pre><code>public TabsAdapter(FragmentManager fm) {
  
      super(fm);
      Log.d("hier128", "hier128");
  }
  
  @Override
  public Fragment getItem(int i) {
  
      Log.d("hier126", "hier126");
  
      Fragment fragment = null;
  
  
      if (i == 0) {
          Log.d("hier130", "hier130");
          fragment = new VorhandeneVorschlaege();
      }
  
  
      return fragment;
  
  }
  
  
  
  @Override
  public int getCount() {
      // get item count - equal to number of tabs
      Log.d("hier129", "hier129");
      return 0;
  }
  
  }
  </code></pre>
  
  <p>And here is the class with the tabs: </p>
  
  <pre><code>public class Vorschlag extends ActionBarActivity implements
      android.support.v7.app.ActionBar.TabListener {
  
  private ViewPager viewPager;
  private TabsAdapter mAdapter;
  //private ActionBar actionBar;
  
  // Tab titles
  private String[] tabs = { "Neuer Vorschlag", "Vorschläge" };
  
  private android.support.v7.app.ActionBar actionBar;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  
  
  
      setContentView(R.layout.vorschlag_layout);
      viewPager = (ViewPager) findViewById(R.id.pager);
  
      FragmentManager fragmnetManager = getSupportFragmentManager();
      actionBar = getSupportActionBar();
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      viewPager.setAdapter(new TabsAdapter(fragmnetManager));
      viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
          @Override
          public void onPageSelected(int arg0) {
              // TODO Auto-generated method stub
              actionBar.setSelectedNavigationItem(arg0);
          }
  
          @Override
          public void onPageScrolled(int arg0, float arg1, int arg2) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onPageScrollStateChanged(int arg0) {
              // TODO Auto-generated method stub
  
          }
      });
  
  
      android.support.v7.app.ActionBar.Tab tab1 = actionBar.newTab();
      tab1.setText("TAB1");
      tab1.setTabListener(this);
  
      android.support.v7.app.ActionBar.Tab tab2 = actionBar.newTab();
      tab2.setText("TAB2");
      tab2.setTabListener(this);
  
  
  
      actionBar.addTab(tab1);
      actionBar.addTab(tab2);
  
  
  }
  
  @Override
  protected void onSaveInstanceState(Bundle outState) {
      // TODO Auto-generated method stub
      super.onSaveInstanceState(outState);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      // Inflate the menu; this adds items to the action bar if it is present.
  
      getMenuInflater().inflate(R.menu.menu_gesture_any_where, menu);
      return true;
  
  }
  
  
  @Override
  public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
                              FragmentTransaction arg1) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
                            FragmentTransaction arg1) {
      // TODO Auto-generated method stub
      viewPager.setCurrentItem(tab.getPosition());
  }
  
  @Override
  public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
                              FragmentTransaction arg1) {
      // TODO Auto-generated method stub
  
  }
  }
  </code></pre>
  
  <p>What is the reason for that? </p>
  
  <p>Thanks</p>