在 Fragment 上调用 RecycleView 时应用崩溃

App crash when invoking RecycleView on Fragment

好的,我在此处 (whosebug.com/questions/27416834/app-crashing-when-trying-to-use-recyclerview-on-android-5-0)、此处 (whosebug.com/questions/26446162/andorid-recyclerview-layoutmanager-exception) 或此处 (code.google.[=37=) 发现了很多与此相关的问题]?id=81588)。我想在那里发表评论,但不能。他们都建议一旦膨胀就设置layoutmanager。我已经完成了,但应用程序仍然崩溃并显示此错误:

java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int) on a null object reference

下面是我的代码片段

MyFragment.java

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.my_fragment, container, false);

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);

        final RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(mLayoutManager);

        myDataset = // initiaized data

        mAdapter = new MyAdapter(getActivity().getApplicationContext(), myDataset);
        mRecyclerView.setAdapter(mAdapter);

        return rootView;
    }

my_fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MyFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

我已经稍微重新格式化了你的代码,它应该可以帮助你进一步调试。

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_fragment, container, false);

        mRecyclerView = (RecyclerView)
            rootView.findViewById(R.id.my_recycler_view);

        if (mRecyclerView != null) {
            mRecyclerView.setLayoutManager(
                new GridLayoutManager(getActivity(), 2));
            mRecyclerView.setHasFixedSize(true);

            myDataset = // initiaized data
            mAdapter = new MyAdapter(
                getActivity().getApplicationContext(), myDataset);
            mRecyclerView.setAdapter(mAdapter);
        } else {
            Log.e("Error", "Unable to find recyclerView");
        }
        return rootView;
    }

我终于找到了解决办法。目前我尝试为 activity 提供导航选项卡。每个选项卡内容都显示在一个片段上。我尝试从 SDK example. The problem is as @nikis said, the adapter didn't call the fragment. According to this tutorial 改编它,我应该使用 extends FragmentPagerAdapter 而不是 PagerAdapter 的适配器,如原始示例所示。这意味着我需要在 FragmentPagerAdapter 上覆盖 getItem 方法,而不是在 PagerAdapter 上覆盖 InstantiateItem 方法。