canScrollHorizontally() crashes if the RecyclerView is not attached to the Window
See original GitHub issueWe have this use case where we might call recyclerView.smoothScrollToPosition
on a view that has been removed from its container. In that case, we get the following crash:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getWidth()' on a null object reference
at com.google.android.flexbox.FlexboxLayoutManager.canScrollHorizontally(FlexboxLayoutManager.java:1900)
at com.google.android.flexbox.FlexboxLayoutManager.getChildWidthMeasureSpec(FlexboxLayoutManager.java:483)
at com.google.android.flexbox.FlexboxHelper.calculateFlexLines(FlexboxHelper.java:458)
at com.google.android.flexbox.FlexboxHelper.calculateHorizontalFlexLines(FlexboxHelper.java:243)
at com.google.android.flexbox.FlexboxLayoutManager.updateFlexLines(FlexboxLayoutManager.java:955)
at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:731)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1858)
at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5044)
Sample code to reproduce the issue => https://github.com/martinbonnin/FlexboxTest. It mainly boils down to:
val recyclerView = RecyclerView(this)
recyclerView.layoutManager = FlexboxLayoutManager(this)
recyclerView.adapter = object: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
[....]
}
recyclerView.smoothScrollToPosition(500)
I understand scrolling on a non-visible View is not the most useful thing to do but I would still expect the LayoutManager to handle this case gracefully ?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
RecyclerView crashes when "scrapped or attached views may ...
This error is caused if in your XML you have android:animateLayoutChanges set to true and you call notifyDataSetChanged() on the RecyclerView's adapter in ......
Read more >recyclerview/recyclerview/src/main/java/androidx ... - Google Git
A flexible view for providing a limited window into a large data set. ... if we are not attached yet, mark us as...
Read more >RecyclerView - Android Developers
Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged has ... Returns true if RecyclerView is attached to...
Read more >Source Code for RecyclerView.java - AndroidX Tech
Attached items are invalid until next layout, at which point layout will ... onMeasure() second time // because getViewForPosition() will crash when LM...
Read more >flexbox-layout/CHANGELOG and flexbox-layout Releases (Page 2 ...
SPACE_EVENLY to IntDef (#489); canScrollHorizontally() throws NPE if the RecyclerView is not attached to the Window (#490); ⚡️ Update the API level to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A workaround is to clone the library and perform a check on line 1901
@Override public boolean canScrollHorizontally() { if (mFlexWrap == FlexWrap.NOWRAP) { return isMainAxisDirectionHorizontal(); } else { return !isMainAxisDirectionHorizontal() || getWidth() > (mParent != null ? mParent.getWidth() : 0); } }
I created a PR for the fix here@bgorkowy I workaround’d the issue for now