-1

I have a bunch of RecyclerViews throughout my application and have been seeing this exception in Fabric for a while now.

Stacktrace:

Fatal Exception: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 8(offset:8).state:11 android.support.v7.widget.RecyclerView{3914473e VFED.... ........ 0,0-1080,1509 #7f11017a app:id/chatlist_recycler_view}, adapter:com.myapp.application.a.c@134b699f, layout:android.support.v7.widget.LinearLayoutManager@1449aec, context:com.myapp.application.activities.HomeActivity@1fcf5f5e
   at android.support.v7.widget.RecyclerView$Recycler.clear(Unknown Source)
   at android.support.v7.widget.GapWorker.add(Unknown Source)
   at android.support.v7.widget.GapWorker.add(Unknown Source)
   at android.support.v7.widget.GapWorker.remove(Unknown Source)
   at android.support.v7.widget.GapWorker.add(Unknown Source)
   at android.support.v7.widget.GapWorker.run(Unknown Source)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5951)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

Looking at this updated stacktrace, it seems like it's an issue with my chatlist_recycler_view.

How should I go about debugging this and fixing it? I don't know what is causing the IndexOutOfBoundsException.

Code from the RecyclerView declaration:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

  <android.support.v7.widget.RecyclerView
  android:id="@+id/chatlist_recycler_view"
  android:background="@android:color/white"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:isScrollContainer="false"/>

</RelativeLayout>

If you guys need any other code please let me know!

Adapter code can be found here:

https://pastebin.com/HzMkCDh5

Walker
  • 1,127
  • 15
  • 39

1 Answers1

4

Well, IndexOutOfBoundsException exception is a common one but in regards to recyclerview here is what you can change to avoid the exception.

1: when you assign the layout adapter just disable predictive animations

   mRecyclerView.setLayoutManager(new LinearLayoutManager(this){
     @Override
    public boolean supportsPredictiveItemAnimations() {
        return false;
    }
});

2: you can try to clear the existing recycler pool and then notify the data change in case of large data set.

mRecyclerView.getRecycledViewPool().clear();
mAdapter.notifyDataSetChanged();

3: dont't use notifyItem.....() use notifyDataSetChanged() only

4: if getting data from API later you can try swapping the adapter again

swapAdapter(adapter, true)

5: make sure you are not scrolling recyclerview to some position on startup this may also cause the index related issues if data is inconsistent.

Link: source

vikas kumar
  • 10,447
  • 2
  • 46
  • 52