FragmentStatePager适配器IndexOutOfBound在背景上

时间:2016-10-03 10:30:25

标签: android android-viewpager

我正在使用扩展FragmentStatePagerAdapter的适配器实现动态viewpager。

每次我的应用程序进入后台时都会崩溃。使用ViewPager上的IndexOutOfBounds:

这是适配器代码:

/**
 * Created by root on 3/11/15.
 */
public class FragmentAdapter extends FragmentStatePagerAdapter {
    private ArrayList<Fragment> mFragList = null;
    private ArrayList<String> mFragmentNameList = null;

    public FragmentAdapter(FragmentManager fm, ArrayList<Fragment> mFragList, ArrayList<String> fragmentNameList) {
        super(fm);
        this.mFragList = mFragList;
        this.mFragmentNameList = fragmentNameList;
    }

    @Override
    public Fragment getItem(int position) {
        if (!AppUtility.isNullOrEmpty(mFragList) && position < mFragList.size()) {
            return mFragList.get(position);
        }
        return null;
    }

    @Override
    public int getCount() {
        if (mFragList != null) {
            return mFragList.size();
        }
        return 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentNameList.get(position);
    }
}

这是片段代码:

 public class PagerFragment extends BaseFragment {

        private final int MAX_PAGES = 4;
        private ViewPager mViewPager = null;
        private FragmentAdapter mFragmentAdapter = null;
        private ArrayList<Fragment> mFragList = new ArrayList<>(MAX_PAGES);
        private ArrayList<String> mFragmentNameList = new ArrayList<>(MAX_PAGES);
        private TabLayout mTabLayout = null;



    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViewById(view);
    }

    public void initViewById(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mTabLayout = (TabLayout) view.findViewById(R.id.tabs);

        mFragList.add(new Fragment());
        mFragList.add(new Fragment());
        mFragList.add(new Fragment());
        mFragList.add(new Fragment());
        mFragmentNameList.add("Fragment1");
        mFragmentNameList.add("Fragment2");
        mFragmentNameList.add("Fragment3");
        mFragmentNameList.add("Fragment4");
        mFragmentAdapter = new FragmentAdapter(getFragmentManager(), mFragList, mFragmentNameList);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(mFragmentAdapter);
        mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        mTabLayout.setupWithViewPager(mViewPager);
    }
}

它没有显示我的类的任何行号,但只显示了viewpager:

错误日志:

FATAL EXCEPTION: main
Process: com.android.myapp.ui, PID: 5897
java.lang.IndexOutOfBoundsException: Invalid index 5, size is 3
  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
  at java.util.ArrayList.get(ArrayList.java:308)
  at android.support.v4.view.ViewPager.getChildDrawingOrder(ViewPager.java:800)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3396)
  at android.view.View.draw(View.java:16384)
  at android.support.v4.view.ViewPager.draw(ViewPager.java:2415)
  at android.view.View.updateDisplayListIfDirty(View.java:15378)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.updateDisplayListIfDirty(View.java:15373)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.draw(View.java:16384)
  at android.view.View.updateDisplayListIfDirty(View.java:15378)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.draw(View.java:16384)
  at android.view.View.updateDisplayListIfDirty(View.java:15378)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.updateDisplayListIfDirty(View.java:15373)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1383)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.draw(View.java:16384)
  at android.view.View.updateDisplayListIfDirty(View.java:15378)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.updateDisplayListIfDirty(View.java:15373)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.updateDisplayListIfDirty(View.java:15373)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.updateDisplayListIfDirty(View.java:15373)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.updateDisplayListIfDirty(View.java:15373)
  at android.view.View.draw(View.java:16151)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
  at android.view.View.draw(View.java:16384)
  at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2692)
  at android.view.View.updateDisplayListIfDirty(View.java:15378)
  at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
  at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
  at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
  at android.view.ViewRootImpl.draw(ViewRootImpl.java:2928)
  at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2740)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2358)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1233)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6549)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:919)
  at android.view.Choreographer.doCallbacks(Choreographer.java:710)
  at android.view.Choreographer.doFrame(Choreographer.java:645)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:905)
    at android.os.Han

2 个答案:

答案 0 :(得分:3)

好的,我找到了解决方案,基本上问题根本不在适配器中,我把我的viewPager放在一个片段中,对于哪个适配器需要引用getChildfragmentManager()而不是getFragmentManager(); 我改变了

mFragmentAdapter = new FragmentAdapter(getFragmentManager(), mFragList, mFragmentNameList);
           to
mFragmentAdapter = new FragmentAdapter(getChildFragmenManager(), mFragList, mFragmentNameList);

和Volla一起工作。

答案 1 :(得分:0)

尝试使用Android调试器来了解当应用程序转到后台时它会崩溃到哪一行然后告诉我们。尝试在onPause()

中做一些事情