title: Android java.lang.IllegalStateException with ViewPager
body: <p>I've made a very simple Android puzzle app with a ViewPager that lets the user swipe through an array of puzzles.  I'm seeing an error in production that I don't know how to reproduce or debug:</p>
  
  <pre><code>java.lang.IllegalStateException: 
    at android.support.v4.view.ViewPager.a (ViewPager.java:204)
    at android.support.v4.view.ViewPager.c (ViewPager.java:2)
    at android.support.v4.view.ViewPager.onMeasure (ViewPager.java:207)
    at android.view.View.measure (View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
    at android.view.View.measure (View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6580)
    at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure (ActionBarOverlayLayout.java:257)
    at android.view.View.measure (View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
    at android.view.View.measure (View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6580)
    at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical (LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure (LinearLayout.java:685)
    at android.view.View.measure (View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure (DecorView.java:721)
    at android.view.View.measure (View.java:22002)
    at android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:2414)
    at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2159)
    at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1390)
    at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6762)
    at android.view.Choreographer$CallbackRecord.run (Choreographer.java:966)
    at android.view.Choreographer.doCallbacks (Choreographer.java:778)
    at android.view.Choreographer.doFrame (Choreographer.java:713)
    at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:952)
    at android.os.Handler.handleCallback (Handler.java:789)
    at android.os.Handler.dispatchMessage (Handler.java:98)
    at android.os.Looper.loop (Looper.java:169)
    at android.app.ActivityThread.main (ActivityThread.java:6595)
    at java.lang.reflect.Method.invoke (Method.java)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)
  </code></pre>
  
  <p>What should I do to figure out how to reproduce this error myself, and to figure out how to fix it?  My android programming skills are fairly limited, and the stack trace above is extremely cryptic to me.</p>
  
  <p>In case it's helpful, here is a second stack trace that appears to be related:</p>
  
  <pre><code>java.lang.IllegalStateException: 
    at android.support.v4.view.ViewPager.access$000 (ViewPager.java)
    or                     .access$200 (ViewPager.java)
    or                     .addNewItem (ViewPager.java)
    or                     .calculatePageOffsets (ViewPager.java)
    or                     .canScroll (ViewPager.java)
    or                     .completeScroll (ViewPager.java)
    or                     .determineTargetPage (ViewPager.java)
    or                     .distanceInfluenceForSnapDuration (ViewPager.java)
    or                     .executeKeyEvent (ViewPager.java)
    or                     .getChildRectInPagerCoordinates (ViewPager.java)
    or                     .infoForChild (ViewPager.java)
    or                     .initViewPager (ViewPager.java)
    or                     .isGutterDrag (ViewPager.java)
    or                     .onPageScrolled (ViewPager.java)
    or                     .onSecondaryPointerUp (ViewPager.java)
    or                     .populate (ViewPager.java)
    or                     .recomputeScrollPosition (ViewPager.java)
    or                     .scrollToItem (ViewPager.java)
    or                     .setCurrentItem (ViewPager.java)
    or                     .setCurrentItemInternal (ViewPager.java)
    or                     .smoothScrollTo (ViewPager.java)
    at android.support.v4.view.ViewPager.arrowScroll (ViewPager.java)
    or                     .populate (ViewPager.java)
    or                     .requestParentDisallowInterceptTouchEvent (ViewPager.java)
    at android.support.v4.view.ViewPager.onMeasure (ViewPager.java)
    at android.view.View.measure (View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6124)
    at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
    at android.view.View.measure (View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6124)
    at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure (ActionBarOverlayLayout.java)
    at android.view.View.measure (View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6124)
    at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
    at android.view.View.measure (View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6124)
    at android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1464)
    at android.widget.LinearLayout.measureVertical (LinearLayout.java:758)
    at android.widget.LinearLayout.onMeasure (LinearLayout.java:640)
    at android.view.View.measure (View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6124)
    at android.widget.FrameLayout.onMeasure (FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure (DecorView.java:687)
    at android.view.View.measure (View.java:19759)
    at android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:2283)
    at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2036)
    at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1258)
    at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6348)
    at android.view.Choreographer$CallbackRecord.run (Choreographer.java:871)
    at android.view.Choreographer.doCallbacks (Choreographer.java:683)
    at android.view.Choreographer.doFrame (Choreographer.java:619)
    at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:857)
    at android.os.Handler.handleCallback (Handler.java:751)
    at android.os.Handler.dispatchMessage (Handler.java:95)
    at android.os.Looper.loop (Looper.java:154)
    at android.app.ActivityThread.main (ActivityThread.java:6123)
    at java.lang.reflect.Method.invoke (Method.java)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)
  </code></pre>
  
  <p>Should I post a shortened/simplified version of my ViewPager code?</p>
  
  <hr>
  
  <p>Edit:  Here's the class that does most of the work:</p>
  
  <pre><code>public class SolvePuzzle extends ActionBarActivity {
  
      // The code is longer than is shown here, but hopefully this is enough to be helpful
      static AppSectionsPagerAdapter mAppSectionsPagerAdapter;
      static ViewPager mViewPager;
      static int level;
      static String images[];
      static String levelDescriptions[];
      static String puzzles[];
      static String answers[];
      static String hints[];
      static String congratulationsArray[];
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_solve_puzzle);
  
          // Some code related to getSupportActionBar(); which I've cut out for brevity
  
          Intent intent = getIntent();
          if (intent != null) {
              level = intent.getIntExtra(PuzzleSelection.LEVEL, 0);  // Possible bug here with default level set to 0?
          }
  
          // Here there's code that populates images, levelDescriptions, puzzles, answers, and other arrays using the level integer
          // The arrays will have different lengths depending on the level
          // i.e. there is a different number of puzzles in each level
          // I do something along the lines of puzzles = getPuzzlesGivenLevel(level); and similarly for answers, hints, etc.
  
          mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
          mViewPager = (ViewPager) findViewById(R.id.pager);
          mViewPager.setAdapter(mAppSectionsPagerAdapter);
          mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
              @Override public void onPageScrollStateChanged(int arg0) {
              }
              @Override public void onPageScrolled(int arg0, float arg1, int arg2) {
              }
              @Override public void onPageSelected(int puzzleIndex) {
          callSetShareIntent(puzzles[puzzleIndex]);  // Make share button share the correct puzzle
              }
          });
          mViewPager.setCurrentItem(indexFirstUnsolvedPuzzle());
      }
  
      private int indexFirstUnsolvedPuzzle() {
      // Gets index of first unsolved puzzle in this level
      }
  
      private void callSetShareIntent(String puzzleStatement) {
      // Creates an Intent called shareIntent; and then calls setShareIntent(shareIntent);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case android.R.id.home:
                  NavUtils.navigateUpFromSameTask(this);
                  return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.solve_puzzle, menu);
          MenuItem item = menu.findItem(R.id.menu_item_share);
  
          // Following http://stackoverflow.com/questions/19118051/unable-to-cast-action-provider-to-share-action-provider
          mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
          if (mShareActionProvider == null) {
              // Following http://stackoverflow.com/questions/19358510/why-menuitemcompat-getactionprovider-returns-null
              mShareActionProvider = new ShareActionProvider(this);
              MenuItemCompat.setActionProvider(item, mShareActionProvider);
          }
          callSetShareIntent(puzzles[mViewPager.getCurrentItem()]);
          return true;  // Return true to display menu
      }
  
      private void setShareIntent(Intent shareIntent) {
          if (mShareActionProvider != null) {
              mShareActionProvider.setShareIntent(shareIntent);  // Should be called whenever new fragment is displayed
          }
      }
  
      public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
  
          public AppSectionsPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int puzzleIndex) {
              Fragment fragment = new SolvePuzzleFragment();
              Bundle args = new Bundle();
              args.putInt(PUZZLE_INDEX, puzzleIndex);
              fragment.setArguments(args);
              return fragment;
          }
  
          @Override
          public int getCount() {
              return puzzles.length;
          }
      }
  
      public static class SolvePuzzleFragment extends Fragment implements OnClickListener {
  
          public double correctAnswer;
          public int puzzleIndex;
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_solve_puzzle, container, false);
              Bundle args = getArguments();
              puzzleIndex = args.getInt(PUZZLE_INDEX);
  
          // Sets a bunch of TextViews using the puzzleIndex
          // For example, get string in puzzles[puzzleIndex] and put it in a TextView, et cetera
          // Set a bunch of onClickListenters
  
      }
  
      // A bunch of functions for checking the user's answer, opening congratulations, etc
      // E.g. public void openCongratulationsAlert(View view) { ... }, public void openIncorrectAnswerToast() { ... }
  
      }
  
  }
  </code></pre>
  
  <p>Please let me know if this needs more explanation.</p>