-2

第二片段我有與我設置的在其三個片段一個片段容器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)

我不能在同一活動中製作新佈局嗎? 是不是新的覆蓋最後?

回答

0

UPDATE
做這個東西,在 從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"); 
} 
+0

沒有成功,仍然崩潰的同樣的錯誤 –

+0

請解決您的main2activity XML文件 –

+0

編輯,對不起,複製粘貼了一些錯誤 –

0

onActivityCreated你並不需要聲明兩次setLayoutManager()方法。你正在做的:

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

取而代之的是,只是聲明:

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

試圖將recyclerView移動數據從onCreateViewonViewCreated

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); 
} 
+0

對不起,我忘了去掉這一,它是沒有成功的嘗試,但問題依然存在。編輯 –

+0

如果可能,請發佈onCreateView方法和xml。 –

+0

已修改:XML和onCreateView已添加 –