第二片段我有與我設置的在其三個片段一個片段容器mainActivity。 在每一個我有一個回收視圖,我移動到下一個片段上的項目點擊。 與recyclerView第一片段被設置如下錯誤與回收視圖佈局管理器
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_courses, container, false);
mRootRef = FirebaseDatabase.getInstance().getReference();
mCoursesList = view.findViewById(R.id.courses_rv);
linearLayoutManager = new LinearLayoutManager(getContext());
mCoursesList.setLayoutManager(linearLayoutManager);
mCoursesList.setHasFixedSize(true);
updateView();
return view;
}
進入其被作爲
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subjects, container, false);
mSubjectsList = view.findViewById(R.id.subjects_rv);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mSubjectsList.setLayoutManager(layoutManager);
mSubjectsList.setHasFixedSize(true);
Bundle bundle = this.getArguments();
if (bundle != null) {
courseName = bundle.getString("CourseName");
subjectName = bundle.getString("SubjectName");
}
// Inflate the layout for this fragment
return view;
}
顯然它們是相同的,但與該錯誤
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
所做的第二個片段,當錯誤發生
XML文件 Main2Activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.crash.mathtest.Views.Main2Activity">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
></LinearLayout>
</LinearLayout>
CoureseFragment:
<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="com.example.crash.mathtest.Views.CoursesFragment">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/courses_rv"
/>
</FrameLayout>
受試者片段是相同的課程片段
再次,這僅出現在第二片段(onCreateView) 錯誤指向setLayoutManager(的LinearLayout)
我不能在同一活動中製作新佈局嗎? 是不是新的覆蓋最後?
沒有成功,仍然崩潰的同樣的錯誤 –
請解決您的main2activity XML文件 –
編輯,對不起,複製粘貼了一些錯誤 –