"recyclerview: No Adapter Attached; Skipping Layout" For Recyclerview In Fragment
I'm getting this error but I don't know what is causing it...perhaps something to do with the fact that this is being initialized in a fragment and not in the activity itself. Edit
Solution 1:
Move your setLayoutManager
line before setAdapter
Solution 2:
Create the adapter and the "non-view" related objects in onCreate
. Then use your adapter for the RecyclerView
.
Also, clean up your code by initiating the objects in a clear order based on their use and the Activity
lifecycle.
publicclassStatsFragmentextendsFragment {
privateRecyclerView mRecyclerView;
privateLinearLayoutManager mLinearLayoutManager;
privateStatsAdapter mAdapter; // was RecyclerView.Adapter mAdapter;privateString[] myDataset = newString[]{"hello", "world", "yolo"};
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1.
mAdapter = newStatsAdapter(myDataset);
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_stats, parent, false);
// 2.
mLinearLayoutManager = newLinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// 3.
mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
Solution 3:
One way to fix this issue is to attach an empty adapter to the RecyclerView
voidinitializeRecyclerView() {
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setAdapter(new SampleAdapter(getCurrentActivity()));
recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity()));
recyclerView.setHasFixedSize(true);
}
Post a Comment for ""recyclerview: No Adapter Attached; Skipping Layout" For Recyclerview In Fragment"