Recyclerview显示空指针异常

时间:2015-08-14 10:52:48

标签: android android-recyclerview gridlayoutmanager

我正在使用带有标签布局的视图寻呼机,而在第三个标签中我使用的是一个包含Recyclerview的片段。 现在,当我滑动标签时,当我到达第三个标签时,它显示错误,

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.stopSmoothScroller()' on a null object reference

我不知道是什么问题,数据从服务器获取但是应用程序崩溃了。我在谷歌搜索了很多相关问题,但没有得到任何有效答案。

这是该片段中recylerview实现的代码。

    recyclerView = (RecyclerView) layout.findViewById(R.id.staggeredGridDesign);
    adapter = new AdapterDesigns(getActivity());
    manager = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(manager);
    recyclerView.swapAdapter(adapter, false);

    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {

            if (m == 1 && listMen.size() % 2 != 0) {
                if (position == 0)
                    return 2;
            } else if (w == 1 && listWomen.size() % 2 != 0) {
                if (position == 0)
                    return 2;
            }
            return 1;
        }
    });

adapter.setDesignList(listMen);

LogCat:

 Process: stvisionary.fashionove.com, PID: 31886
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.stopSmoothScroller()' on a null object reference
        at android.support.v7.widget.RecyclerView.stopScrollersInternal(RecyclerView.java:1229)
        at android.support.v7.widget.RecyclerView.stopScroll(RecyclerView.java:1221)
        at android.support.v7.widget.RecyclerView.onDetachedFromWindow(RecyclerView.java:1426)
        at android.view.View.dispatchDetachedFromWindow(View.java:13459)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2839)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2836)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2836)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2836)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2836)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2836)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4165)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4138)
        at android.view.ViewGroup.removeView(ViewGroup.java:4070)
        at android.support.v4.view.ViewPager.removeView(ViewPager.java:1326)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1055)
        at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1233)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:709)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:549)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

08 -

1 个答案:

答案 0 :(得分:0)

我认为问题不在这个片段中(根本不是第三个)。

at android.support.v4.view.ViewPager.removeView(ViewPager.java:1326)

根据您的logcat,之前关闭其中一个片段存在问题。基本上是在试图打电话

'void android.support.v7.widget.RecyclerView$LayoutManager.stopSmoothScroller()'

片段分离后

,此循环器视图不再存在。因此,看一看并确保不会发生这样的事情可能是个好主意。

无论如何,您可以通过缓存它们来阻止破坏ViewPager中的碎片,如

viewPager.setOffscreenPageLimit(5);