0

Some of our users (around 2%) are getting an error that I cannot reproduce on any physical and virtual device:

The line where this error corresponds is the last one of this piece of code:

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

mPager is a ViewPager. I am replicating fragment 1 to 3 because the infinite view pager cannot have less than 4 fragment and I need to have 3. The CustomInfinitePagerAdapter is:

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;
    }
}

Any idea?

Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
  • http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Samuel Tulach Mar 17 '17 at 20:37
  • Samuel read my question. If I am posting this is because I need help. Not because I don't know what causes a NullPointerExeption. So please don't down vote my question. – Damia Fuentes Mar 17 '17 at 20:41
  • Maybe try to add `CustomInfinitePagerAdapter mAdapter;` before `mAdapter = new CustomInfinitePagerAdapter(getChildFragmentManager(), fragments);`. I can't test it, because I am not currently on PC. – Samuel Tulach Mar 17 '17 at 20:48
  • Please post stack trace of exception... – samus Mar 17 '17 at 20:58
  • 1
    Samuel Tulach, of course that's done before. Samuel Arin question is updated. Thanks. – Damia Fuentes Mar 17 '17 at 21:01
  • The exception states that a size() is being called on a null object. The line you are pointing out does not have a list. In the code the only possibility is that if fragments is null. – bichito Mar 17 '17 at 23:45
  • I know efekctive. That's why is so confusing and that's why I posted it here. I don't know why I am getting that error in that line. Maybe someone can help me. – Damia Fuentes Mar 19 '17 at 19:46

0 Answers0