viewPager.setAdapter(mAdapter)上java.util.ArrayList.size()的NullPoiterException

时间:2017-03-17 20:33:18

标签: android android-fragments arraylist android-viewpager

我们的一些用户(约2%)收到了我无法在任何物理和虚拟设备上重现的错误:

此错误对应的行是此段代码的最后一行:

List<Fragment> fragments = new ArrayList<>();
fragments.add(Fragment1.newInstance(data1));
fragments.add(Fragment2.newInstance(data2));
fragments.add(Fragment3.newInstance(data3));
fragments.add(Fragment1.newInstance(data1));
fragments.add(Fragment2.newInstance(data2));
fragments.add(Fragment3.newInstance(data3));
CustomInfinitePagerAdapter mAdapter = new CustomInfinitePagerAdapter(getChildFragmentManager(), fragments);
mPager.setAdapter(mAdapter); //<-- Here is where our users have the crash

mPagerViewPager。我正在复制片段1到3,因为无限视图寻呼机不能少于4个片段,我需要有3. CustomInfinitePagerAdapter是:

public class CustomInfinitePagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFrags = new ArrayList<Fragment>();

    public CustomInfinitePagerAdapter(FragmentManager fm, List<Fragment> frags) {
        super(fm);
        mFrags = frags;
    }

    @Override
    public Fragment getItem(int position) {
        if (mFrags == null || mFrags.size() == 0)
            return null;
        while (position < 0)
            position += mFrags.size();

        int index = position % mFrags.size();
        return mFrags.get(index);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }
}

有什么想法吗?

0 个答案:

没有答案