在Fragment上调用RecycleView时应用程序崩溃

时间:2015-03-25 04:33:54

标签: android android-recyclerview

好的,我在这里找到了很多相关问题(stackoverflow.com/questions/27416834/app-crashing-when-trying-to-use-recyclerview-on-android-5-0),这里( stackoverflow.com/questions/26446162/andorid-recyclerview-layoutmanager-exception)或此处(code.google.com/p/android/issues/detail?id=81588)。我想在那里评论,但不能。他们所有人都建议尽快设置布局管理器。我已经完成了,应用程序仍然崩溃并显示此错误:

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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

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

    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;
    }

答案 1 :(得分:0)

最后我找到了解决方案。目前,我尝试为活动提供导航标签。每个选项卡内容都显示在片段上。我尝试从SDK example改编它。问题是@nikis说,适配器没有调用片段。根据{{​​3}},我应该使用extends FragmentPagerAdapter代替PagerAdapter的适配器,如原始示例所示。这意味着我需要在getItem上覆盖FragmentPagerAdapter方法而不是InstantiateItem上的PagerAdapter