"recyclerview.sethasfixedsize(boolean)' On A Null Object Reference" Recyclerview Into Fragment
I've a tabbed activity, I want to show in the first fragment (Tab1History) a Recyclerview and Cardview. I've created a XML file for CardView, and in the fragment XML I've insert th
Solution 1:
Change this line :
recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView);
To this:
recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView);
Also, you are using the same layout for your RecyclerView.Adapter
and your Fragment
in this line:
ViewrootView= inflater.inflate(R.layout.cespite_card_view, container, false);
You should have a layout file for your fragment that contains your RecyclerView
widget inside it, like this:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e9e9e9 />
Solution 2:
Change getView()
into rootView
getView
- Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ViewrootView= inflater.inflate(R.layout.cespite_card_view, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView);
recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size
recyclerView.setLayoutManager(newLinearLayoutManager(getContext()));
cespiteOggList = newArrayList<>();
for(int i=0; i<=10; i++)
{
CespiteOggcespiteOgg=newCespiteOgg("heading"+(i+1), "Lorem ipsum", date, "sdjijsad", "jdkjd");
cespiteOggList.add(cespiteOgg);
}
adapter = newCespiteAdapter(cespiteOggList, getContext());
recyclerView.setAdapter(adapter);
return rootView;
}
Solution 3:
You should find your view from inflated view. Use:
recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView);
instead of:
recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView);
Good luck.
Post a Comment for ""recyclerview.sethasfixedsize(boolean)' On A Null Object Reference" Recyclerview Into Fragment"