带有回收站视图布局管理器的第二个片段出错

时间:2017-08-25 04:35:31

标签: android android-fragments android-recyclerview recyclerview-layout

我有一个带有片段容器的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)

我不能在同一个活动中制作新的布局吗? 新的不会覆盖最后一个吗?

2 个答案:

答案 0 :(得分:0)

<强>更新
做这些东西 来自onCreateView的onActivityCreated

    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");
}

答案 1 :(得分:0)

您不需要声明两次setLayoutManager()方法。你在做:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mSubjectsList.setLayoutManager(layoutManager);
mSubjectsList.setLayoutManager(mSubjectsList.getLayoutManager());

而不是这个,只需声明:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mSubjectsList.setLayoutManager(layoutManager);

尝试将recyclerView数据从onCreateView移至onViewCreated

内部onCreateView()方法:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.your_fragment, container, false);
}

现在将您的recyclerView内容添加到onViewCreated()方法:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerViewId);

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mSubjectsList.setLayoutManager(layoutManager);
}