-2

I load recycler normally and data is shown properly. But I have requirement to recreate my actvity on certain condition. When I recreate my activity, Recycler view is initialized again and is loaded with new set of data. But this time I get a crash. Below are the logs. Any help would be really appreciated.. !!!

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: start < 0 || end > len. start=-1, end=11, len=11
       at java.util.Arrays.checkStartAndEnd(Arrays.java:1732)
       at java.util.Arrays.fill(Arrays.java:803)
       at android.support.v7.widget.StaggeredGridLayoutManager$LazySpanLookup.invalidateAfter(StaggeredGridLayoutManager.java:2652)
       at android.support.v7.widget.StaggeredGridLayoutManager.handleUpdate(StaggeredGridLayoutManager.java:1496)
       at android.support.v7.widget.StaggeredGridLayoutManager.onItemsUpdated(StaggeredGridLayoutManager.java:1472)
       at android.support.v7.widget.RecyclerView$6.dispatchUpdate(RecyclerView.java:781)
       at android.support.v7.widget.RecyclerView$6.onDispatchFirstPass(RecyclerView.java:769)
       at android.support.v7.widget.AdapterHelper.dispatchFirstPassAndUpdateViewHolders(AdapterHelper.java:316)
       at android.support.v7.widget.AdapterHelper.dispatchAndUpdateViewHolders(AdapterHelper.java:302)
       at android.support.v7.widget.AdapterHelper.applyUpdate(AdapterHelper.java:222)
       at android.support.v7.widget.AdapterHelper.preProcess(AdapterHelper.java:106)
       at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1492)
       at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:151)
       at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
       at android.view.Choreographer.doFrame(Choreographer.java:603)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Vadims Savjolovs
  • 2,218
  • 22
  • 47
user3492435
  • 846
  • 2
  • 10
  • 24
  • You are getting a **ArrayIndexOutOfBoundsException**. That means that you are trying to access an Array item which is not there in the Array. Similar question has been answered here - http://stackoverflow.com/questions/35577548/recycler-view-fatal-exception-java-lang-arrayindexoutofboundsexception – Asutosh Panda Mar 03 '17 at 11:41
  • and what is not clear in the error message `java.lang.ArrayIndexOutOfBoundsException: start < 0 || end > len. start=-1, end=11, len=11`? – Vladyslav Matviienko Mar 03 '17 at 11:49
  • I am not doing anything unusual. Even though I am still getting crash. – user3492435 Mar 03 '17 at 12:41
  • @user3492435 check my answer, late but i hope that fix your crash. – Khaled Lela Nov 14 '17 at 12:13

2 Answers2

0

In my case, I was trying to notifyItemChanged(Default_pos), where Default_pos was -1 which doesnot exist.

Hence I was getting ArrayIndexOutOfBound.

user3492435
  • 846
  • 2
  • 10
  • 24
0

When You are try to recreate your activity, RecyclerView still trying to display view items and ComputingLayout

RecyclerView is in a lockdown state and any attempt to update adapter contents will result in an exception

Solution

  • You should just postpone recreate activity using a Handler.
  • Check RecyclerView.isComputingLayout() should return false before you fire your change.

For more details Check my answer

Khaled Lela
  • 5,495
  • 5
  • 39
  • 63