Android RecyclerView - clearOldPositions - NullPointerExceptions

时间:2015-02-12 10:10:26

标签: java android android-recyclerview

我使用支持包中的RecyclerView遇到了一个非常奇怪的问题。

RecyclerView内有ViewPagerViewPager显示RecyclerView时,应用程序崩溃并显示以下堆栈跟踪:

java.lang.NullPointerException
            at android.support.v7.widget.RecyclerView.clearOldPositions(RecyclerView.java:2378)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1968)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.widget.ScrollView.onLayout(ScrollView.java:1468)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

我在互联网上找不到任何有关此内容的消息来源,所以如果有人在此之前遇到此问题,我们将不胜感激。

一些背景知识:

我正在build.gradle中导入RecyclerView,如下所示:

compile 'com.android.support:recyclerview-v7:+'

我正在将Adapter和LayoutManager设置为RecyclerView构造函数中的RecyclerView,如下所示:

    public CategoryParallaxView(Context context)
    {
        super(context);
        init();
    }

    public CategoryParallaxView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public CategoryParallaxView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    private void init()
    {
        mLayoutManager = new CategoryParallaxLayoutManager();
        setLayoutManager(mLayoutManager);

        mCategoryList = new ArrayList<CartCategory>();
        mAdapter = new CategoryParallaxAdapter();
        setAdapter(mAdapter);
    }

当显示RecyclerView时,数据源(mCategoryList)的大小为0,但列表本身不为空。

RecyclerView嵌套在LinearLayout内(它本身嵌套在ScrollView中),并且在宽度和高度上定义了MATCH_PARENT。从一些调试来看,似乎RecyclerView计算的宽度和高度是正确的。

不确定这是否相关,但在我的实现中,AdapterViewHolderLayoutManager子类都是my RecyclerView子类的私有内部类。

1 个答案:

答案 0 :(得分:0)

问题是RecyclerViewLayoutManagerRecyclerView显示ViewPager时,正在清除我在init方法中设置的LayoutManager

我通过在RecyclerView变得可见后立即分配{{1}}来修复此问题。感觉有点hacky,但我找不到任何其他工作。如果有人能找到更好的解决方案,请发帖。