question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

canScrollHorizontally() crashes if the RecyclerView is not attached to the Window

See original GitHub issue

We 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
KryptKodecommented, Apr 19, 2019

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

0reactions
martinbonnincommented, Feb 14, 2019

@bgorkowy I workaround’d the issue for now

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found