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 296 forks source link

NPE at LayoutManager#onItemsUpdated #228

Open jihun-im opened 6 years ago

jihun-im commented 6 years ago

This is NPE crash log

--------- beginning of crash 06-25 19:54:19.959 4842 4842 E AndroidRuntime: FATAL EXCEPTION: main 06-25 19:54:19.959 4842 4842 E AndroidRuntime: Process: jp.naver.line.android, PID: 4842 06-25 19:54:19.959 4842 4842 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.RecyclerView$LayoutManager.getPosition(RecyclerView.java:8294) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at com.linecorp.linekeep.opensrc.com.tonicartos.superslim.LayoutManager.onItemsUpdated(LayoutManager.java:545) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.RecyclerView$LayoutManager.onItemsUpdated(RecyclerView.java:9728) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.RecyclerView$6.dispatchUpdate(RecyclerView.java:943) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.RecyclerView$6.onDispatchFirstPass(RecyclerView.java:931) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.AdapterHelper.dispatchFirstPassAndUpdateViewHolders(AdapterHelper.java:314) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.AdapterHelper.dispatchAndUpdateViewHolders(AdapterHelper.java:300) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.AdapterHelper.applyUpdate(AdapterHelper.java:220) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.AdapterHelper.preProcess(AdapterHelper.java:104) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1753) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:930) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:705) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:637) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:916) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:751) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6823) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557) 06-25 19:54:19.959 4842 4842 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

It looks like getChildAt(0) sometimes return null. Do we need to handle NPE here?

    @Override
    public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) {
        super.onItemsUpdated(recyclerView, positionStart, itemCount);

        View first = getChildAt(0);
        View last = getChildAt(getChildCount() - 1);
        if (positionStart + itemCount <= getPosition(first)) {
            return;
        }

        if (positionStart <= getPosition(last)) {
            requestLayout();
        }
    }