Reputation: 3215
Okay, I've found many issue about this here (stackoverflow.com/questions/27416834/app-crashing-when-trying-to-use-recyclerview-on-android-5-0), here (stackoverflow.com/questions/26446162/andorid-recyclerview-layoutmanager-exception), or here (code.google.com/p/android/issues/detail?id=81588). I want to comment there, but can't. All of them suggest to set layoutmanager as soon as it inflated. I've done it and the app is still crash and showing this error:
java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int) on a null object reference
below is my code snippet
MyFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
final RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
myDataset = // initiaized data
mAdapter = new MyAdapter(getActivity().getApplicationContext(), myDataset);
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
my_fragment.xml
<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=".MyFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Upvotes: 1
Views: 801
Reputation: 3215
Finally I've found a solution. Currently I try to provide navigation tab to an activity. Each tab content is shown on a fragment. I try to adapt it from SDK example. The problem is as @nikis said, the adapter didn't call the fragment. According to this tutorial, I should use adapter that extends FragmentPagerAdapter
instead of PagerAdapter
as shown in original example. That means that I need to override getItem
method on FragmentPagerAdapter
instead of InstantiateItem
on PagerAdapter
.
Upvotes: 1
Reputation: 3332
I've reformatted your code a little, it should help you debug further.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment, container, false);
mRecyclerView = (RecyclerView)
rootView.findViewById(R.id.my_recycler_view);
if (mRecyclerView != null) {
mRecyclerView.setLayoutManager(
new GridLayoutManager(getActivity(), 2));
mRecyclerView.setHasFixedSize(true);
myDataset = // initiaized data
mAdapter = new MyAdapter(
getActivity().getApplicationContext(), myDataset);
mRecyclerView.setAdapter(mAdapter);
} else {
Log.e("Error", "Unable to find recyclerView");
}
return rootView;
}
Upvotes: 1