带有 Recyclerview 的 ViewPager 上的 NullPointerException

Posted

技术标签:

【中文标题】带有 Recyclerview 的 ViewPager 上的 NullPointerException【英文标题】:NullPointerException on ViewPager with Recyclerview 【发布时间】:2015-12-17 22:25:24 【问题描述】:

我们的应用程序上有一个ViewPager 和一个FragmentPagerAdapter,其中包含三个片段。其中两个片段由一个Recyclerview 组成。

第一页(没有 ViewPager 的片段)正确显示。但是,当 ViewPager 尝试预加载下一页(RecyclerView)时,应用程序会因为NullPointerException 而崩溃,日志如下:

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
         at android.support.v7.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:2839)
         at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2626)
         at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1000)
         at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:710)
         at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:724)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
         at android.view.View.layout(View.java:15684)
         at android.view.ViewGroup.layout(ViewGroup.java:4981)
         at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
         at android.view.Choreographer.doCallbacks(Choreographer.java:596)
         at android.view.Choreographer.doFrame(Choreographer.java:557)
         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
         at android.os.Handler.handleCallback(Handler.java:739)
         at android.os.Handler.dispatchMessage(Handler.java:95)
         at android.os.Looper.loop(Looper.java:155)
         at android.app.ActivityThread.main(ActivityThread.java:5696)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)

ViewPager 的声明方式如下:

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(fragment1, "fragment1");
adapter.addFrag(fragment2, "fragment2");
adapter.addFrag(fragment3, "fragment3");
viewPager.setAdapter(adapter);

还有适配器:

    private class ViewPagerAdapter extends FragmentPagerAdapter 
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    public ViewPagerAdapter(FragmentManager manager) 
        super(manager);
    
    @Override
    public Fragment getItem(int position) 
        return mFragmentList.get(position);
    
    @Override
    public int getCount() 
        return mFragmentList.size();
    
    public void addFrag(Fragment fragment, String title) 
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    
    @Override
    public CharSequence getPageTitle(int position) 
        return mFragmentTitleList.get(position);
    

由于两个 RecyclerView 的代码都很长并且每个页面都不同,我真的不知道哪个部分是相关的,所以我不会给出任何示例。如果您认为对解决问题有帮助,请随时提出具体要求。

我可以告诉你的一件事是,如果我想让它工作,我必须评论来自 RecylerView 的每个 setAdapter 的调用。

编辑:这是第二页的代码。

public class MyFragment extends Fragment 

    RecyclerView recyclerView;
    GridAdapter gridAdapter;

    public GridAdapter getGridAdapter() 
        return gridAdapter;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        final View v = inflater.inflate(R.layout.our_layout, container, false);
        recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        gridLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(gridLayoutManager);

        recyclerView.setHasFixedSize(true);

        return v;
    

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);
        ArrayList<Model> model = getArguments().getParcelableArrayList("extra");
        if (model != null && model.size() != 0) 
            gridAdapter = new GridAdapter(model);
            recyclerView.setAdapter(gridAdapter);
        
    

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) 
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isResumed())
            onResume();
        
    

    @Override
    public void onResume() 
        super.onResume();
        if (!getUserVisibleHint())
            return;
    

    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration 

        private int spanCount;
        private int spacingLeft;
        private int spacingRight;
        private int spacingTop;
        private int spacingBottom;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacingLeft, int spacingTop, int spacingRight, int spacingBottom, boolean includeEdge) 
            this.spanCount = spanCount;
            this.spacingLeft = spacingLeft;
            this.spacingRight = spacingRight;
            this.spacingTop = spacingTop;
            this.spacingBottom = spacingBottom;
            this.includeEdge = includeEdge;
        

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) 
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) 
                outRect.left = spacingLeft - column * spacingLeft / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacingRight / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount)  // top edge
                    outRect.top = spacingTop;
                
                outRect.bottom = spacingBottom; // item bottom
             else 
                outRect.left = column * spacingLeft / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacingRight - (column + 1) * spacingRight / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) 
                    outRect.top = spacingTop; // item top
                
            
        
    

    public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> 

        private ArrayList<Model> model;

        public GridAdapter(ArrayList<Model> offer) 
            super();
            model = offer;
        

        @Override
        public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) 
            final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
            final ViewHolder holder = new ViewHolder(view);
            return holder;
        

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) 
            final Model currentOffer = model.get(position);

            holder.category.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() 
                @SuppressLint("NewApi")
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() 
                    int width = holder.category.getWidth();
                    ViewGroup.LayoutParams params = holder.appIcon.getLayoutParams();
                    params.width = width;
                    params.height = width;

                    holder.appIcon.setLayoutParams(params);

                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
                        holder.itemView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    else
                        holder.itemView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                
            );

            Picasso.with(getActivity().getApplicationContext()).
                    load(currentOffer.getApp_logo()).fit().centerCrop().into(holder.appIcon);
            holder.appName.setText(currentOffer.getApp_name());
            holder.category.setText(currentOffer.getApp_category());

            holder.itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    String marketURL = AndroidTools.getPlayStoreURL(currentOffer.getApp_store_id(), true);

                    UITools.launchUrl(getActivity(), marketURL);

                
            );

        

        @Override
        public int getItemCount() 
            return model.size();
        

        class ViewHolder extends RecyclerView.ViewHolder 
            private ImageView appIcon;
            private TextView appName;
            private TextView category;

            public ViewHolder(View itemView) 
                super(itemView);
                appIcon = (ImageView)itemView.findViewById(R.id.item_icon);
                appName = (TextView)itemView.findViewById(R.id.item_app_name);
                category = (TextView)itemView.findViewById(R.id.item_category);
            
        
    

非常感谢任何帮助。

【问题讨论】:

第三个片段有没有recyclerView? [EDITED]对不起,它在那里。你能发布你的第二个片段的代码 首页是一个没有任何RecyclerView的片段。但是,是的,第二个和第三个是片段,每个片段都由一个 RecyclerView 组成。 您可以发布您的适配器的onBindViewHolder() 方法吗? 请发布您的第二个片段的代码 附加到片段的数据是来自本地或静态数据还是来自服务器的数据 【参考方案1】:

我在开发过程中遇到了这个错误。您是否检查过您的 XML 文件中的 RecyclerView 是否正确包装到另一个布局中,例如 FrameLayout?

如果不是,它只会在 Viewpager 上崩溃,而不会在单个片段视图上崩溃。

【讨论】:

你是对的!我的布局只由一个 RecyclerView 组成,我不知道它必须包装在另一个中。感谢您的帮助! 在我的情况下,错误是由于不同的原因引起的。在这里查看我的答案:***.com/a/47436165/700693 我的 RecyclerView 中有一个随机的 TextView,只能在搞砸它的 XML 中查看。 你拯救了我的一天! :-) 但是“正确包装到另一个布局”是什么意思?有这方面的官方文档吗?【参考方案2】:

当您不小心将视图直接添加到 RecyclerView 时,就会发生这种情况。在我的例子中,我使用View.inflate 作为装饰器布局,将RecyclerView 作为父参数,它会自动附加它。 RecyclerView 遍历附加到它的所有子视图,并期望其所有视图子视图在布局参数中具有 ViewHolders,并且当子视图持有者为空时将抛出此 NPE。

【讨论】:

【参考方案3】:

当您在 xml 布局 文件中直接在 listViewRecyclerView 下添加 元素时,会发生这种情况。

<android.support.v7.widget.RecyclerView
android:layout_
android:layout_
android:scrollbars="vertical">

<TextView
    android:layout_
    android:layout_ /> 

 </android.support.v7.widget.RecyclerView>

在这里,我在 RecyclerView 中添加了一个 TextView,它会抛出我 onLayout error(由 NullPointerException 引起)。你不应该直接在 RecyclerViewlistView 下添加元素。

【讨论】:

这对我来说是个错误。删除子 RecyclerView 的元素后,它就像一个魅力。这个NullPointerException 是一个描述性错误。【参考方案4】:

在回收站视图中添加 no children 并将 attachToRootinflate() 方法的第三个参数)设置为 false,同时为我添加自定义布局.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.online_user, parent, false);
    return new RecyclerViewHolder(view.findViewById(R.id.onlineUserView));

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/onlineUsersView"
    android:layout_
    android:layout_ />

</LinearLayout>

【讨论】:

【参考方案5】:

变化:

 private final List<Fragment> mFragmentList = new ArrayList<>();
 private final List<String> mFragmentTitleList = new ArrayList<>();

到:

 // remove final keyword
 private List<Fragment> mFragmentList = new ArrayList<>();
 private List<String> mFragmentTitleList = new ArrayList<>();

最终变量不会取任何值,即使您尝试添加它们。

【讨论】:

哦。然后不用一一添加,而是将完整的列表发送给Adapter,在adapter中全部添加到列表中。 这不是“最终变量”的工作方式。您可以在列表中添加和删除项目,即使它已声明为最终项目。 是的,斯科特说的;列表不是它拥有的对象;将这些对象分类为一个集合是最终的。想象一下,您将如何根据列表的最终确定性来断言列表中项目的字段是最终的,您就会明白为什么在保持任何表面性能的同时无法做到这一点。

以上是关于带有 Recyclerview 的 ViewPager 上的 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现

我无法从另一个带有 recyclerview.adaper 的片段中打开带有 recyclerview.adapter 的片段

Android tab导航的几种方法:ActionBar tab +fragment,Viewpager+pagerTitleStrip,开源框架ViewPageIndicator 和 ViewPag

带有 RecyclerView 和折叠标题的 CoordinatorLayout

带有片段的 RecyclerView

带有水平溢出的Android垂直recyclerview