0

I add a OnPageChangeListener in my ViewPager in activity.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mPageAdapter = new PageAdapter(fragmentManager);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mPageAdapter);
    mPager.setOffscreenPageLimit(CACHE_SIZE);
    mPager.addOnPageChangeListener(mOnPageChangeListener);
    ...


private ViewPager.SimpleOnPageChangeListener mOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int i) {
        Log.d(TAG, "this: " + this);
        ChartFragment chartFragment = mFragmentList.get(i).second; // crash when activity recreate............
        ...

And I store all fragments in a List.

private List<Pair<String, ChartFragment>> mFragmentList = new ArrayList<Pair<String, ChartFragment>>();

private class PageAdapter extends FragmentPagerAdapter {
    public Object instantiateItem(ViewGroup container, int position) {
        mFragmentList.add(new Pair<String, ChartFragment>(...));

Now I stop my app process with DDMS and reopen it, this page crashes in function onPageSelected.
I don't know why the function onPageSelected exec before ViewPager "init".
How can I fix this? Help!

log:

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
MyActivity$1.onPageSelected(MyActivity.java:101)
...

the onPageSelected function call in ViewPager.onRestoreInstanceState.

10-15 13:22:57.745 D/MyAccountActivity( 8998):  at ...MyActivity$1.onPageSelected(MyActivity.java:103)
10-15 13:22:57.745 D/MyAccountActivity( 8998):  at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1792)
10-15 13:22:57.745 D/MyAccountActivity( 8998):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:547)
10-15 13:22:57.745 D/MyAccountActivity( 8998):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
10-15 13:22:57.745 D/MyAccountActivity( 8998):  at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1319)
android_su
  • 1,471
  • 2
  • 20
  • 29
  • @mr.icetea the log says that "mFragmentList size is 0", because instantiateItem doesn't run yet. – android_su Oct 14 '15 at 09:57
  • If your crash is because of indexoutofBounds then check the like "mFragmentList.get(i).second". – Raghu Mudem Oct 14 '15 at 09:58
  • @RaghuRamiReddy I know that of course. I just wanna know why the function onPageSelected exec before ViewPager "init". – android_su Oct 14 '15 at 10:00
  • @RaghuRamiReddy If ViewPager finishes init, then run the onPageSelected, everything will be fine. – android_su Oct 14 '15 at 10:01
  • in your case mPager = (ViewPager) findViewById(R.id.pager); is initializing ViewPager and onPageSelected is calling for the first page on activity load. – Raghu Mudem Oct 14 '15 at 10:11
  • Solution for you is: make sure data is available in "mFragmentList" before adding PageChangeListener for ViewPager. – Raghu Mudem Oct 14 '15 at 10:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92244/discussion-between-raghu-rami-reddy-and-android-su). – Raghu Mudem Oct 14 '15 at 10:20

1 Answers1

0

Make sure data is available in "mFragmentList" before adding PageChangeListener for ViewPager.

Get data load in "mFragmentList" before the following line

mPager.addOnPageChangeListener(mOnPageChangeListener);
Raghu Mudem
  • 6,218
  • 12
  • 42
  • 62
  • Thanks. But how... That's what I want to know. Where should I call addOnPageChangeListener. – android_su Oct 14 '15 at 10:24
  • I need to wait all items in ViewPager instantiated. I don't find a callback function for this. – android_su Oct 14 '15 at 10:27
  • Please add your "PageAdapter". That will be helpful to identify where your "mFragmentList" is filling with data. – Raghu Mudem Oct 14 '15 at 13:15
  • Can you please show when "instantiateItem()" is calling – Raghu Mudem Oct 15 '15 at 09:36
  • Instead of adding fragments into list, you can add one more method to get the current fragment like this. Please look "getFragment()" method in the following link answer: http://stackoverflow.com/questions/7379165/update-data-in-listfragment-as-part-of-viewpager – Raghu Mudem Oct 15 '15 at 10:01