【发布时间】: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;
}
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); 返回了什么,它是空的?
我不确定是什么原因造成的。对类似问题的其他搜索没有找到任何修复方法。
修复:
似乎将 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