I am getting an error saying "No adapter attached; skipping layout", however my images are loading into my LayoutManager just fine. However, when i try to scroll down and load additional data, the application crashes with a NullPointerException. Here is my related code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
service.getPodcasts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(podcasts -> mRecyclerView.setAdapter(new PodcastsAdapter(getActivity(), podcasts)));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_podcasts, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.podcasts_recycler_view);
mLayoutManager = new GridLayoutManager(getActivity(), 3);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//mRecyclerView.setAdapter(mAdapter);
return rootView;
}
Any ideas on how to fix this?
You didn't attach the adapter because you create it after you try to attach it:
In my case:
mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
Related
Previous Question:
May I know why this error will occur when I try to start RecyclerView activity doing a search from fragment.
After added adapter:
error: OwnerDAO.getOwner()' on a null object reference
Fragment code:
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
OwnerDAO mOwnerDao;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.activity_search, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this.getActivity());
mOwnerDao = new OwnerDAO(getContext())
adapter = new SearchAdapter(getContext(),mOwnerDao.getOwner());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter( adapter );
return v;
}
Search Adapter:
public SearchAdapter(Context context, List<Owner> owners) {
this.context = context;
this.owners = owners;
}
Set the layoutManager after setAdapter like this :-
recyclerView.setAdapter( adapter );
recyclerView.setLayoutManager(layoutManager);
The error will be removed !!
Recyclerview in my Fragment is giving me this error but I couldn't figure it out. There are lots of questions about this issue but none of them solved my problem so far.
Here is a piece of code from my fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
initCollapsingToolbar();
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareSectors();
try {
Glide.with(this).load(R.drawable.cover).into((ImageView) view.findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
You did not create an instance of your adapter before setting it to RecyclerView.
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
// Your adapter initialization here
adapter = new YourAdapter(getActivity(), ....);
recyclerView.setAdapter(adapter);
UPDATE:
Get LayoutInflater from passed context. Update your SectorAdapter code portion as below:
public Context mContext;
public List<Sector> sectorList;
LayoutInflater layoutInflater;
public SectorAdapter(Context mContext, List<Sector> sectorList) {
this.mContext = mContext;
this.sectorList = sectorList;
layoutInflater = LayoutInflater.from(mContext);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.sector_card, parent, false);
return new MyViewHolder(itemView);
}
Try moving your RecyclerView codes to onViewCreated()
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sectorList = new ArrayList<>();
prepareSectors();
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new SectorAdapter(getActivity(), sectorList);
recyclerView.setAdapter(adapter);
}
Hope this will help~
There is no any code for your adapter. Thus you need to implement the adapter and pass the instance to the recyclerView.
RecyclerView.LayoutManager mLayoutManager ....
....
// Your adapter initialization here
adapter = new someAdapter(....);
recyclerView.setAdapter(adapter);
This will help you!
public class WorkFragment extends Fragment {
List<CardViewItem> items = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("FRAGMENT", "Work Fragment started");
TypedArray icons = getResources().obtainTypedArray(R.array.project_icons);
TypedArray names = getResources().obtainTypedArray(R.array.project_names);
TypedArray descs = getResources().obtainTypedArray(R.array.project_descs);
for (int i=0;i<icons.length();i++){
items.add(new CardViewItem(icons.getDrawable(i),
names.getString(i),
descs.getString(i)));
}
icons.recycle();
names.recycle();
descs.recycle();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("FRAGMENT", "Work Fragment onCreateView");
View rootView = inflater.inflate(R.layout.fragment_work, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new ItemAdapter(items));
return inflater.inflate(R.layout.fragment_work, container, false);
}
}
Gives me
E/RecyclerView: No adapter attached; skipping layout
I have tried out all the solutions I had found (setting an empty adapter, moving the code elswhere, using a seperate thread) but to no avail. This should work on a normal activity so I guess maybe I'm doing something wrong.
the problem is mostly this line
return inflater.inflate(R.layout.fragment_work, container, false);
you should have
return rootView
With the first return you are inflating a new totally different view hierarchy, starting from fragment_work.xml, from the one which has an the RecyclerView correctly set up.
In my fragment's onCreateView method, I am inflating a layout that contains a RecyclerView. However, when I try to reference the view, I get a NullPointerException at the following line:
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
What am I doing wrong?
Here is my code:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstance){
final StaggeredGridLayoutManager mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.list);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setHasFixedSize(true); //Data size is fixed - improves performance
mAdapter = new TravelListAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
return inflater.inflate(R.layout.start_fragment,container,false);
}
You are doing getActivity.findViewById(), which wouldn't work. You just created the view, but it is not attached to the activity yet. For this to work, you have to find the view in the fragment's view, not the activity's view.
First, inflate the fragment's view, and then do inflatedView.findViewById(R.id.list).
Example:
public View onCreateView(...){
View inflatedView = inflater.inflate(R.layout.start_fragment, container, false);
mRecyclerView = (RecyclerView) inflatedView.findViewById(R.id.list);
return inflatedView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
final StaggeredGridLayoutManager mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) view.findViewById(R.id.list);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setHasFixedSize(true);
mAdapter = new TravelListAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
super.onViewCreated(view, savedInstanceState);
}
this is what I would do
This question already has answers here:
recyclerview No adapter attached; skipping layout
(38 answers)
Closed 3 years ago.
logcat error : No adapter attached; skipping layout
I changed the context argument with getActivity(), but the app is still not running.
public class FragmentActivity extends Fragment {
private RecyclerView mRecyclerView;
private CountryAdapter mAdapter;
private LinearLayoutManager layoutManager;
public FragmentActivity(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
You didn't attach the adapter because you create it after you try to attach it:
mRecyclerView.setAdapter(mAdapter); // Here, mAdapter is null
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
In my case this problem occurs because there was a view on my layout which moved my list container and it was displayed too small