title: FragmentPagerAdapter: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents
body: <p>Trying to create application with view pager inside navigation drawer. I've options to select from navigation and depending on that (outer selection), i have to populate different number of fragments in view pager. </p>
  
  <p>I've used tutorials to create navigation drawer and view pager separately from Google docs. As per docs, view pager have fixed number of fragments. if i hard code that way everything works fine.</p>
  
  <p>But, when i try to make it dynamic as per left selection, i'm getting illegal state exception</p>
  
  <p>for instance, I've implemented fragmentstatepageradapter,</p>
  
  <pre><code> public class innerFragment extends Fragment {
  
      SectionsPagerAdapter mSectionsPagerAdapter;
      ViewPager mViewPager;
      public static final String active_outer_option = "section_number";
  
      public innerFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_outer, container,
                  false);
  
          Log.d("logs", "onCreateView:innerFragment()");
  
          mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
          mViewPager.setAdapter(mSectionsPagerAdapter);
  
          return rootView;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          ((MainActivity) activity).onSectionAttached(Integer
                  .parseInt(getArguments().getString(active_outer_option)));
          Log.d("logs", "onAttach:innerFragment()");
      }
  
      public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
          private int active_outer_option= 0;
  
          public SectionsPagerAdapter(FragmentManager fm) {
              super(fm);
  
          }
  
          @Override
          public Fragment getItem(int position) {
              Log.d("logs", "getItem(int " + position + ")");
  
              active_outer_option = position;
              Bundle b = new Bundle();
              b.putString("section_number", "" + position);
              DummySectionFragment fragment = new DummySectionFragment();
              fragment.setArguments(b);
              return fragment;
          }
  
          @Override
          public int getCount() {
              switch (active_outer_option) {
              case 0:
                  return 9;
              case 1:
                  return 2;
              case 2:
                  return 2;
              case 3:
                  return 2;
              case 4:
                  return 4;
              default:
                  return 0;
              }
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              Log.d("logs", "getPageTitle(int " + position + ")");
              return "OP " + String.valueOf(position);
          }
  
      }
  
      public static class DummySectionFragment extends Fragment {
          public static final String ARG_SECTION_NUMBER = "section_number";
  
          public DummySectionFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
  
              Log.d("logs", "onCreateView:DummySectionFragment()");
  
              View rootView = inflater.inflate(R.layout.fragment_inner,
                      container, false);
              TextView dummyTextView = (TextView) rootView
                      .findViewById(R.id.section_label);
              dummyTextView.setText(getArguments().getString(ARG_SECTION_NUMBER));
              return rootView;
          }
  
      }
  
  }
  </code></pre>
  
  <p>when i run this application, its giving "Unfortunately application stopped working" and exception is:</p>
  
  <pre><code>java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 9, found: 2 Pager id: com.xxx.xxx:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.xxx.xxx.innerFragment$SectionsPagerAdapter
  </code></pre>
  
  <p>any idea whats wrong with that exception and code?</p>
  
  <p>Edit:</p>
  
  <p>from my debugging first everything is fine and gettitle is called twice with index 0 and 1 and after that getcount is called with index  and getting exception.</p>
  
  <p>does this ring any bells?</p>