title: Force close on orientation change with ViewPager/Fragments/ViewPagerExtensions
body: <p>I'm having a bit of an issue I can't seem to work around (I haven't worked with fragments in this way before)</p>
  
  <p>I have a main FragmentActivity as follows:</p>
  
  <pre><code>package com.my.app;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface; 
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.view.ViewPager;
  
  import com.astuetz.viewpager.extensions.ScrollingTabsView;
  import com.astuetz.viewpager.extensions.TabsAdapter;
  
  public class MyTabsActivity extends FragmentActivity {
  
  private ViewPager mPager;
  private ScrollingTabsView mScrollingTabs;
  
  private TabsAdapter mScrollingTabsAdapter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my_tabs);
  
      mPager = (ViewPager) findViewById(R.id.pager);
      mScrollingTabs = (ScrollingTabsView) findViewById(R.id.my_tabs);
      mScrollingTabsAdapter = new MyTabsAdapter(this, mPager);
      addTabs();
      mScrollingTabs.setAdapter(mScrollingTabsAdapter);
      mScrollingTabs.setViewPager(mPager);
      mPager.setCurrentItem(1);
      mPager.setPageMargin(1);
  }
  
  private void addTabs() {
      mScrollingTabsAdapter.addTab("FirstFragment", "First");
      mScrollingTabsAdapter.addTab("SecondFragment", "Second");
  }
  }
  </code></pre>
  
  <p>The XML is as follows (with the Viewpager and ScrollingTabsView:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res/com.my.package"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" &gt;
  
  &lt;com.astuetz.viewpager.extensions.ScrollingTabsView
      android:id="@+id/ztc_tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/tab_unselected_holo"
      app:dividerDrawable="@drawable/divider"/&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_below="@+id/ztc_tabs" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Then in the MyTabsAdapter I have this:</p>
  
  <pre><code>package com.my.app;
  
  import com.my.app.R;
  
  import java.util.ArrayList;
  
  import android.app.Activity;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.Button;
  
  import com.astuetz.viewpager.extensions.TabsAdapter;
  
  public class MyTabsAdapter extends FragmentPagerAdapter implements TabsAdapter {
  private final ArrayList&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;();
  private final ViewPager mPager;
  
  private Activity mContext;
  
  static final class TabInfo {
      private final String fragName;
      private final String title;
  
      TabInfo(String fragName, String title) {
          this.fragName = fragName;
          this.title = title;
      }
  }
  
  public MyTabsAdapter(FragmentActivity ctx, ViewPager pager) {
      super(ctx.getSupportFragmentManager());
      this.mContext = ctx;
      this.mPager = pager;
      this.mPager.setAdapter(this);
  }
  
  public View getView(int position) {
      Button tab;
  
      LayoutInflater inflater = mContext.getLayoutInflater();
      tab = (Button) inflater.inflate(R.layout.tab_scrolling, null);
  
      tab.setText(this.mTabs.get(position).title.toUpperCase());
  
      return tab;
  }
  
  public void addTab(String fragName, String title) {
      TabInfo info = new TabInfo(fragName, title);
      mTabs.add(info);
  }
  
  @Override
  public Fragment getItem(int position) {
      TabInfo info = mTabs.get(position);
      if (info.fragName == "FirstFragment") {
          return FirstFragment.newInstance();
      } else if (info.fragName == "SecondFragment") {
          return SecondFragment.newInstance(t);
      }
      return null;
  }
  
  @Override
  public int getCount() {
      return mTabs.size();
  }
  
  }
  </code></pre>
  
  <p>And FirstFragment/SecondFragment have their own basic layouts and are nearly identical, heres a Fragment</p>
  
  <pre><code>package com.my.app;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class FirstFragment extends Fragment {
  
  public static Fragment newInstance() {
      FirstFragment frag = new FirstFragment();;
      return frag;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {
      if (container == null)
          return null;
      return inflater.inflate(R.layout.fragment_first, container, false);
  }
  
  }
  </code></pre>
  
  <p>Everything here works exactly as I want it to. The tabs work, the ViewPager pages properly, etc.</p>
  
  <p>The problem is, I get this trace and force close when I change the orientation and I'm not sure why.</p>
  
  <pre><code>E/AndroidRuntime(32431): FATAL EXCEPTION: main
  E/AndroidRuntime(32431): java.lang.NullPointerException
  E/AndroidRuntime(32431):    at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
  E/AndroidRuntime(32431):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
  E/AndroidRuntime(32431):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
  E/AndroidRuntime(32431):    at android.app.Activity.performSaveInstanceState(Activity.java:1147)
  E/AndroidRuntime(32431):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1216)
  E/AndroidRuntime(32431):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3666)
  E/AndroidRuntime(32431):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
  E/AndroidRuntime(32431):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
  E/AndroidRuntime(32431):    at android.os.Handler.dispatchMessage(Handler.java:99)
  E/AndroidRuntime(32431):    at android.os.Looper.loop(Looper.java:137)
  E/AndroidRuntime(32431):    at android.app.ActivityThread.main(ActivityThread.java:5039)
  E/AndroidRuntime(32431):    at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(32431):    at java.lang.reflect.Method.invoke(Method.java:511)
  E/AndroidRuntime(32431):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  E/AndroidRuntime(32431):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  E/AndroidRuntime(32431):    at dalvik.system.NativeStart.main(Native Method)
  W/ActivityManager(  418):   Force finishing activity com.ada.ztc/.ZTCTabs
  </code></pre>
  
  <p>So what I need help with is figuring out why I'm getting that error on orientation change. I've done some research and haven't seemed to find anybody with a similar setup.</p>
  
  <p>Any help would be appreciated, I'd be glad to provide more information if needed.</p>