flexbox-layout: 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)
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 27 (1 by maintainers)
Commits related to this issue
- fix crash https://github.com/google/flexbox-layout/issues/363 — committed to tipa/flexbox-layout by tipa 2 years ago
I have the same problem and it often crash my app. Please fix this issue.
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)
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
+1
This is annoying problem. Hope for quick fix
Same problem, waiting for the fix
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?
Reproduced today.