RecyclerView上的findViewById始终为null

时间:2016-03-02 05:48:18

标签: android xml android-fragments nullpointerexception

我在Fragment中有以下代码段:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_smartzone_category, container, false);

    final Context context = getActivity();

    mPresenter = new SmartzonePresenter(context, this);
    //
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner);
    mProgressBar.setVisibility(View.VISIBLE);

    // set list
    mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    mStaggeredLayoutManager.setSpanCount(3);

    mRecyclerView = (RecyclerView) root.findViewById(R.id.smartzonecategorylist);
    mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
    mRecyclerView.setVisibility(View.INVISIBLE);;

    // display list
    mListAdapter = new VideoCategoryAdapter(context, mCategories);
    mRecyclerView.setAdapter(mListAdapter);
    mListAdapter.setOnItemClickListener(new VideoCategoryAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            // get the category
            VideoCategory vc = mListAdapter.getItem(position);
            // display the sub-smartzone category page
            mCallback.onSelectCategory(vc, mCategories);
        }
    });

    //
    retrieveVideoList();

    return root;
}

mRecyclerView类的标题处声明Fragment

布局对应于此XML文件:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<ProgressBar
    android:id="@id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyle"
    android:layout_centerInParent="true" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/smartzonecategorylist"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</FrameLayout>

然而,在清理/重建之后,我不断收到此Logcat错误:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                         at com.kidmixapp.commoncode.fragments.SmartzoneCategoryFragment.onCreateView(SmartzoneCategoryFragment.java:125)

这是指这一行:

mRecyclerView.setLayoutManager(mStaggeredLayoutManager);

我还用调试行做了一些评估,看看root.findViewById(R.id.smartzonecategorylist);返回了什么,它是否为空?

我不确定是什么原因造成的。对类似问题的其他搜索没有找到我能找到的修复。

FIX:

似乎正在更改XML布局以引用@id/android:list并将findViewById更改为findViewById(android.R.id.list);,就像修改它一样。我不知道为什么。

新XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<ProgressBar
    android:id="@id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>

<android.support.v7.widget.RecyclerView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

NEW FRAGMENT ONCREATEVIEW

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_smartzone_category, container, false);

    final Context context = getActivity();

    mPresenter = new SmartzonePresenter(context, this);
    //
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner);
    mProgressBar.setVisibility(View.VISIBLE);

    // set list
    mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    mStaggeredLayoutManager.setSpanCount(3);

    mRecyclerView = (RecyclerView) root.findViewById(android.R.id.list);
    mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
    mRecyclerView.setVisibility(View.INVISIBLE);;

    // display list
    mListAdapter = new VideoCategoryAdapter(context, mCategories);
    mRecyclerView.setAdapter(mListAdapter);
    mListAdapter.setOnItemClickListener(new VideoCategoryAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            // get the category
            VideoCategory vc = mListAdapter.getItem(position);
            // display the sub-smartzone category page
            mCallback.onSelectCategory(vc, mCategories);
        }
    });

    //
    retrieveVideoList();

    return root;
}

1 个答案:

答案 0 :(得分:-1)

在您的布局XML中,您的代码是:

<ProgressBar
android:id="@id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true" />

但我认为代码应该是:

<ProgressBar
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true" />