我的代碼在Fragment
以下部分:findViewById上RecyclerView總是空
@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>
新的片斷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'佈局 – Mohit
是的,它屬於該佈局。 – Tukajo
嘗試清理和重建您的項目 –