flexbox-layout
flexbox-layout copied to clipboard
ArrayIndexOutOfBoundsException in updateLayoutState()
Hi, I have a crash from time to time when updating data. It looks like updateLayoutState()
throws an IndexOutOfBoundsException
here:
FlexboxLayoutManager
line 2057
int firstVisiblePosition = getPosition(firstVisible);
int firstVisibleLinePosition = mFlexboxHelper.mIndexToFlexLine[firstVisiblePosition];
getPosition(firstVisible)
seems to return -1 (NO_POSITION) sometimes. Maybe this is related to the activity being hidden behind another. It happens at least on versions 0.3.0 and 0.3.1
Here is the full stacktrace:
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
at com.google.android.flexbox.FlexboxLayoutManager.updateLayoutState(FlexboxLayoutManager.java:2058)
at com.google.android.flexbox.FlexboxLayoutManager.handleScrollingCrossAxis(FlexboxLayoutManager.java:1930)
at com.google.android.flexbox.FlexboxLayoutManager.fixLayoutEndGap(FlexboxLayoutManager.java:825)
at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:762)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.CardView.onMeasure(CardView.java:218)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1293)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:340)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1675)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
Thanks for filing this.
I really appreciate if you can share the code snippet or an example project that can reproduce the issue. Is it possible?
I have the same issue with same stack trace in my project. But I couldn't find stable way to reproduce it.
I use notifyDataSetChanged() and the crash happens rarely after data change. I have a RecyclerView with FlexboxLayoutManager inside AppBarLayout with CoordinatorLayout.
seems same problem java.lang.ArrayIndexOutOfBoundsExceptionlength=10; index=-1 com.google.android.flexbox.FlexboxLayoutManager.void setFlexDirection(int)(Unknown Source)
Can't reproduce eigther
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.FLEX_START);
mListView.setLayoutManager(layoutManager);
I'm having the same crash in Line 1368: int currentLineIndex = mFlexboxHelper.mIndexToFlexLine[getPosition(lastView)];
This happens when a layout occurs while the fragment is obscured by other fragments.
The code looks like it could use a little index safety... getPosition appears to return -1 a lot and it's used without checks all through this class
I'm dealing with a similar problem:
Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=20; index=-1 at com.google.android.flexbox.FlexboxLayoutManager$AnchorInfo.assignFromView(FlexboxLayoutManager.java:2863) at com.google.android.flexbox.FlexboxLayoutManager$AnchorInfo.access$700(FlexboxLayoutManager.java:2792) at com.google.android.flexbox.FlexboxLayoutManager.updateAnchorFromChildren(FlexboxLayoutManager.java:1136) at com.google.android.flexbox.FlexboxLayoutManager.updateAnchorInfoForLayout(FlexboxLayoutManager.java:1032) at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:700) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812) at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225) at android.view.View.measure(View.java:18804) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18804) at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
FlexboxLayoutManager use cachedViews which ViewHolder has been reseted. This my caused by RecyclerView is not visible
this is my workaround.
mListView.setItemViewCacheSize(10);
adapter.setHasStableIds(true);
The FlexBoxLayoutManager doesn't seem to work well if your RecyclerViewAdapter doesn't call setStableIds(true). This means it also has to implement getItemId(int position), and return something meaningful (an actual unique ID).
If you do this, performance will be heaps better, animations will start working, and the thing will stop crashing all the time.
Potentially, as a fix, this library should enforce the use of stable IDs somehow?
I have the same problem and it often crash my app. Please fix this issue.
+1
I faced it because the RecyclerView not visible. firstVisiblePosition=-1.
Same problem, waiting for the fix
This is annoying problem. Hope for quick fix
+1
Same problem, but this workaround does the job for now 👍
FlexboxLayoutManager use cachedViews which ViewHolder has been reseted. This my caused by RecyclerView is not visible
this is my workaround.
mListView.setItemViewCacheSize(10); adapter.setHasStableIds(true);
Same issue. And the mentioned workaround didn't help me in my scenario.
same problem + 1. version:1.0.0
I've downloaded sources and changed some code for dealing with this issue. I've changed file FlexboxLayoutManager.java(line 1536): addView(view);
to try { addView(view); } catch (Exception e) {}
. And I've changed condition if (position == 0) { return; }
to if (position <= 0) { return; }
in file FlexboxItemDecoration.java (line 106). Know, it is not best solution, but seems it works without issues. Maybe this solution helps somebody.
Its being 4 years this issue is open, any plans to fix this?
Reproduced today.
Reproduced today.
Version: 2.0.1
same problem when I have rotated the screen. worked for me! you just
adapter.setHasStableIds(true)
and override this method in your adapter.
override fun getItemId(position: Int): Long {
return position.toLong()
}
I have same issue, version 3.0.0
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
at com.google.android.flexbox.FlexboxLayoutManager.updateLayoutState(FlexboxLayoutManager.java:2132)
at com.google.android.flexbox.FlexboxLayoutManager.handleScrollingMainOrientation(FlexboxLayoutManager.java:1993)
at com.google.android.flexbox.FlexboxLayoutManager.fixLayoutEndGap(FlexboxLayoutManager.java:845)
at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:782)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
+1
I have similar issue
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1 at java.util.ArrayList.get(ArrayList.java:439) at com.google.android.flexbox.FlexboxLayoutManager.findLastReferenceChild(FlexboxLayoutManager.java:1211) at com.google.android.flexbox.FlexboxLayoutManager.computeScrollOffset(FlexboxLayoutManager.java:2251) at com.google.android.flexbox.FlexboxLayoutManager.computeVerticalScrollOffset(FlexboxLayoutManager.java:2238) at androidx.recyclerview.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:2206) at android.view.View.canScrollVertically(View.java:19156)
Same here
I have similar issue
Fatal Exception: java.lang.IndexOutOfBoundsException: Index: 18, Size: 18
at java.util.ArrayList.get(ArrayList.java:437)
at com.google.android.flexbox.FlexboxLayoutManager.findFirstReferenceChild(FlexboxLayoutManager.java:29)
at com.google.android.flexbox.FlexboxLayoutManager.updateAnchorFromChildren(FlexboxLayoutManager.java:446)
at com.google.android.flexbox.FlexboxLayoutManager.updateAnchorInfoForLayout(FlexboxLayoutManager.java:446)
at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:446)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:92)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:125)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:8)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at androidx.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:392)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:392)
at com.google.android.material.bottomsheet.BottomSheetBehavior.onLayoutChild(BottomSheetBehavior.java:161)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:43)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1729)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1638)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:799)
at android.view.View.layout(View.java:23716)
at android.view.ViewGroup.layout(ViewGroup.java:6413)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4097)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3438)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2350)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9194)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239)
at android.view.Choreographer.doCallbacks(Choreographer.java:899)
at android.view.Choreographer.doFrame(Choreographer.java:832)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7884)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
The same crash, v3.0.0 Please fix it I have RecyclerView in RecyclerView, used FlexboxLayoutManager. setHasStableIds with getItemId didn't help
Stacktrace: Fatal Exception: java.lang.IndexOutOfBoundsException Index: 2, Size: 1 java.util.ArrayList.get (ArrayList.java:437) com.google.android.flexbox.FlexboxLayoutManager.findFirstReferenceChild (FlexboxLayoutManager.java:1199) com.google.android.flexbox.FlexboxLayoutManager.updateAnchorFromChildren (FlexboxLayoutManager.java:1157) com.google.android.flexbox.FlexboxLayoutManager.updateAnchorInfoForLayout (FlexboxLayoutManager.java:1052) com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren (FlexboxLayoutManager.java:721) androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:4645)
android.view.ViewGroup.layout (ViewGroup.java:6505) com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild (HeaderScrollingViewBehavior.java:149) com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild (ViewOffsetBehavior.java:43) com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild (AppBarLayout.java:2348) androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout (CoordinatorLayout.java:953)