【问题标题】:findViewById on RecyclerView always nullRecyclerView 上的 findViewById 始终为空
【发布时间】:2016-06-14 20:19:37
【问题描述】:

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

mRecyclerViewFragment 类的标头中声明。

布局对应这个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); 返回了什么,它是空的?

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

修复:

似乎将 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>

新片段 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;
}

【问题讨论】:

  • 是你的smartzonecategorylist 属于fragment_smartzone_category 布局
  • 是的,它属于那个布局。
  • 尝试清理并重建您的项目
  • @BurhanuddinRashid 我提到我已经尝试过几次。没有运气。
  • @Tukajo 你确定你已经为这个片段添加了正确的布局吗?

标签: android xml android-fragments nullpointerexception


【解决方案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" />

【讨论】:

  • 我会改变这个,但是这会导致问题,因为RecyclerView 正在抛出null
  • 两者有什么区别?
  • 当我做出这个改变时;我现在在 findViewById(R.id.spinner); 处都得到一个空指针错误。和我的另一个错误。似乎将其更改为“@+id”会导致空值,任何想法为什么?
猜你喜欢
  • 2016-08-25
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2019-10-21
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多