1

我正在使用带有选项卡布局的视图寻呼机,在第三个选项卡中我使用的是一个包含 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);

日志猫:

 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-

4

1 回答 1

0

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

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

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

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

在片段已经分离之后,这个回收器视图不再存在。因此,看一看,并确保不会发生类似的事情可能是个好主意。

无论如何,您可以通过像这样缓存它们来防止破坏 ViewPager 中的片段

viewPager.setOffscreenPageLimit(5);

于 2015-08-14T20:18:28.597 回答