-2

I have a mainActivity with a fragment container which i set one of three fragments in it. In each one i have a recycler view and I move to the next fragment on an item click. The first fragment with the recyclerView is set as follows

    @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;
}

the error happens when entering the second fragment which is done as

    @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;
}

Apparently they are the same but with the error

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

The XML files 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>

Subjects fragment is the same as courses fragment

Again, this only appears on the second fragment (onCreateView) The error points to the setLayoutManager(linearLayout)

Can't I make new layouts in the same activity ? doesn't the new one override the last ?

2 Answers2

0

UPDATE
do this stuff in onActivityCreated from onCreateView

    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");
}
jigar savaliya
  • 474
  • 1
  • 8
  • 21
0

You don't need to declare twice setLayoutManager() method. You are doing:

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

Instead of this, just declare:

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

Try to move your recyclerView data from onCreateView to onViewCreated.

Inside onCreateView() method:

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

Now add your recyclerView content to onViewCreated() method:

@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);
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53