Skip to content

Fatal Exception: java.lang.IndexOutOfBoundsException Position must be [0, adapter.getItemCount()] #35

Open
samardzija opened this issue Jun 11, 2021 · 4 comments

Comments

@samardzija
Copy link

Hi,

I'm using ViewPager2 with ListAdapter in RecycleView. Every time data is updated, view is attached to viewPager, again. When changing adapter data, setting a new list, a crash from the title occurs.
Do you know the reason why?

Full log:
ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.setCurrentPosition (ScrollingPagerIndicator.java:390)
ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher.updateIndicatorDotsAndPosition (ViewPager2Attacher.java:66)
ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher.attachToPager (ViewPager2Attacher.java:23)
ru.tinkoff.scrollingpagerindicator.ViewPager2Attacher.attachToPager (ViewPager2Attacher.java:7)
ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager (ScrollingPagerIndicator.java:297)
ru.tinkoff.scrollingpagerindicator.ScrollingPagerIndicator.attachToPager (ScrollingPagerIndicator.java:247)

@nikhilwynk
Copy link

@samardzija I am also facing this issue please share the resolution if you find one

@samardzija
Copy link
Author

samardzija commented Jun 12, 2021

@nikhilwynk The problem is that viewpager current position has been remembered. That's why a crash occurs.
I've added a listener to the adapter, to notify me when adapter data is changed and in that case, I set viewPager current position to 0.
So, first extend constructor for adapter and add listener:
ViewPagerAdapter(val onDataChange: Runnable)

Then, when listener triggers, set:
yourViewPager.currentItem = 0

@nikhilwynk
Copy link

@samardzija Thanks for the inputs but I don't think this is actually what causes the crash as adapter data does not change in my case and it always remains the same. Moreover how often do you get the crash ? I am getting like 15 crashes a day out of 5.5M Daily Active users . Also can you reproduce this issue manually ? I am not able to reproduce this manually.

@samardzija
Copy link
Author

samardzija commented Jun 14, 2021

Wow, that's nasty. Yes, I can reproduce it. My use case is that I have view pager with adapter attached. Adapter data is changed, based on some things that happen behind. I can reproduce it by:

  • adapter size is bigger than one, swipe to the second position, or third position, whatever
  • trigger adapter change so that it will have only one element
  • view pager remembers that it should be in the second position and when data is changed, it crashes.

It is strange that adapter data is not changing in your case, I can't think of the way how you could reproduce the issue.
Maybe is something happening with the data, try to set don't keep activity in Settings.

Anyway, I don't think library should crash at this moment, anyway. It should just ignore.

        if (position != 0 && (position < 0 || position >= itemCount)) {
            throw new IndexOutOfBoundsException("Position must be [0, adapter.getItemCount()]");
        }
        if (itemCount == 0) {
            return;
        }
        adjustFramePosition(0, position);
        updateScaleInIdleState(position);
    }    

this is the method that crash. Maybe, if (itemCount == 0) should be before throwing exception.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants