TonicArtos / SuperSLiM

A layout manager for the RecyclerView with interchangeable linear, grid, and staggered displays of views, all with configurable section headers including the sticky variety as specified in the material design docs.
http://tonicartos.nz
2.12k stars 297 forks source link

NPE when calling findLastCompletelyVisibleItemPosition #161

Closed frazer-rbsn closed 7 years ago

frazer-rbsn commented 8 years ago
 Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
   at com.tonicartos.superslim.SectionData.(SectionData.java:36)
   at com.myapp.app.misc.RecyclerView.com.tonicartos.superslim.LayoutManager.findLastCompletelyVisibleItemPosition(RecyclerView.java:1212)
   at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3665)
   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2824)
   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
   at android.view.View.layout(View.java:15614)
   at android.view.ViewGroup.layout(ViewGroup.java:4968)

This only happens when we redraw the layout after a screen rotation. It doesn't happen every time and doesn't happen for every user, which makes me think it could be a race condition. But a colleague with a slow device can reproduce it almost every time.

Here's how we are calling the code:

@Override
public void onScrolled(int dx, int dy) {
    super.onScrolled(dx, dy);
    if(transcriptMode == TRANSCRIPT_MODE_NORMAL) {
        com.tonicartos.superslim.LayoutManager layoutManager = ((com.tonicartos.superslim.LayoutManager) getLayoutManager());
        if (layoutManager.getItemCount() == 0) return;

        forceTranscriptScroll = layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1;
    }
}

We are using version 0.4.13 and it has been seen on Android 4 and 5.

gatorboy commented 7 years ago

Happens for me too. @TonicArtos Any plan on fixing this?

sofiahellofresh commented 7 years ago

We also have this issue. @frazer-rbsn @gatorboy have you found a work around for it?

gatorboy commented 7 years ago

@sofiahellofresh I was able to write some workaround for this..

                final View view = layoutManager.getChildAt(layoutManager.getChildCount() - 1);
                int lastVisiblePosition;
                if (view == null) {
                    lastVisiblePosition = 0;
                } else {
                    lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
                    if (lastVisiblePosition == LayoutManager.INVALID_POSITON) {
                        lastVisiblePosition = 0;
                    }
                }