recyclerview No adapter attached; skipping layout

匿名 (未验证) 提交于 2019-12-03 01:31:01

问题:

Just implemented Recyclerview in my code, replacing Listview.

everything works fine. The objects are displayed.

but logcat says

15:25:53.476 E/RecyclerView: No adapter attached; skipping layout

15:25:53.655 E/RecyclerView: No adapter attached; skipping layout

for the code

ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists); recyclerView = (RecyclerView) findViewById(R.id.cardList); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); 

As you can see I have attached an adapter for Recycleview. so why do I keep getting this error?

i have read other questions related to same problem but none helps.

回答1:

Can you make sure that you are calling these statements from the "main" thread (for example inside the onCreate() method). As soon as I call the same statements from a "delayed" method. In my case a ResultCallback, I get the same message.

In my Fragment, calling the code below from inside a ResultCallback method produces the same message. After moving the code to the onConnected() method within my app, the message was gone...

LinearLayoutManager llm = new LinearLayoutManager(this); llm.setOrientation(LinearLayoutManager.VERTICAL); list.setLayoutManager(llm); list.setAdapter( adapter ); 


回答2:

I was getting the same two error messages until I fixed two things in my code:

(1) By default, when you implement methods in the RecyclerView.Adapter it generates:

 @Override     public int getItemCount() {         return 0;     } 

Make sure you update your code so it says:

 @Override     public int getItemCount() {         return artists.size();     } 

Obviously if you have zero items in your items then you will get zero things displayed on the screen.

(2) I was not doing this as shown in the top answer: CardView layout_width="match_parent" does not match parent RecyclerView width

//correct LayoutInflater.from(parent.getContext())             .inflate(R.layout.card_listitem, parent, false);  //incorrect (what I had) LayoutInflater.from(parent.getContext())         .inflate(R.layout.card_listitem,null); 

(3) EDIT: BONUS: Also make sure you set up your RecyclerView like this:

            

NOT like this:

            

I have seen some tutorials using the latter method. While it works I think it generates this error too.



回答3:

I have the same situation with you, display is ok, but error appear in the locat. That's my solution: (1) Initialize the RecyclerView & bind adapter ON CREATE()

RecyclerView mRecycler = (RecyclerView) this.findViewById(R.id.yourid); mRecycler.setAdapter(adapter); 

(2) call notifyDataStateChanged when you get the data

adapter.notifyDataStateChanged(); 

In the recyclerView's source code, there is other thread to check the state of data.

public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle);     this.mObserver = new RecyclerView.RecyclerViewDataObserver(null);     this.mRecycler = new RecyclerView.Recycler();     this.mUpdateChildViewsRunnable = new Runnable() {         public void run() {             if(RecyclerView.this.mFirstLayoutComplete) {                 if(RecyclerView.this.mDataSetHasChangedAfterLayout) {                     TraceCompat.beginSection("RV FullInvalidate");                     RecyclerView.this.dispatchLayout();                     TraceCompat.endSection();                 } else if(RecyclerView.this.mAdapterHelper.hasPendingUpdates()) {                     TraceCompat.beginSection("RV PartialInvalidate");                     RecyclerView.this.eatRequestLayout();                     RecyclerView.this.mAdapterHelper.preProcess();                     if(!RecyclerView.this.mLayoutRequestEaten) {                         RecyclerView.this.rebindUpdatedViewHolders();                     }                      RecyclerView.this.resumeRequestLayout(true);                     TraceCompat.endSection();                 }              }         }     }; 

In the dispatchLayout(), we can find there is the error in it:

void dispatchLayout() {     if(this.mAdapter == null) {         Log.e("RecyclerView", "No adapter attached; skipping layout");     } else if(this.mLayout == null) {         Log.e("RecyclerView", "No layout manager attached; skipping layout");     } else { 


回答4:

i have this problem , a few time problem is recycleView put in ScrollView object

After checking implementation, the reason appears to be the following. If RecyclerView gets put into a ScrollView, then during measure step its height is unspecified (because ScrollView allows any height) and, as a result, gets equal to minimum height (as per implementation) which is apparently zero.

You have couple of options for fixing this:

  1. Set a certain height to RecyclerView
  2. Set ScrollView.fillViewport to true
  3. Or keep RecyclerView outside of ScrollView. In my opinion, this is the best option by far. If RecyclerView height is not limited - which is the case when it's put into ScrollView - then all Adapter's views have enough place vertically and get created all at once. There is no view recycling anymore which kinda breaks the purpose of RecyclerView .


回答5:

It happens when you are not setting the adapter during the creation phase:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity);     mRecyclerView.setLayoutManager(new LinearLayoutManager(this));     .... }  public void onResume() {     super.onResume();     mRecyclerView.setAdapter(mAdapter);     .... } 

Just move setting the adapter into onCreate with an empty data and when you have the data call:

mAdapter.notifyDataSetChanged(); 


回答6:

1) Create ViewHolder that does nothing :)

// SampleHolder.java public class SampleHolder extends RecyclerView.ViewHolder {     public SampleHolder(View itemView) {         super(itemView);     } } 

2) Again create RecyclerView that does nothing :)

// SampleRecycler.java public class SampleRecycler extends RecyclerView.Adapter {     @Override     public SampleHolder onCreateViewHolder(ViewGroup parent, int viewType) {         return null;     }      @Override     public void onBindViewHolder(SampleHolder holder, int position) {      }      @Override     public int getItemCount() {         return 0;     } } 

3) Now when your real recycler is not ready just use the sample one like below.

RecyclerView myRecycler = (RecyclerView) findViewById(R.id.recycler_id); myRecycler.setLayoutManager(new LinearLayoutManager(this)); myRecycler.setAdapter(new SampleRecycler()); 

This is not best solution though but it works! Hope this is helpful.



回答7:

Make sure you set the layout manager for your RecyclerView by:

mRecyclerView.setLayoutManager(new LinearLayoutManager(context)); 

Instead of LinearLayoutManager, you can use other layout managers too.



回答8:

I had the same error I fixed it doing this if you are waiting for data like me using retrofit or something like that

Put before Oncreate

private ArtistArrayAdapter adapter; private RecyclerView recyclerView; 

Put them in your Oncreate

 recyclerView = (RecyclerView) findViewById(R.id.cardList);  recyclerView.setHasFixedSize(true);  recyclerView.setLayoutManager(new LinearLayoutManager(this));  adapter = new ArtistArrayAdapter( artists , R.layout.list_item ,getApplicationContext());  recyclerView.setAdapter(adapter); 

When you receive data put

adapter = new ArtistArrayAdapter( artists , R.layout.list_item ,getApplicationContext()); recyclerView.setAdapter(adapter); 

Now go in your ArtistArrayAdapter class and do this what it will do is if your array is empty or is null it will make GetItemCount return 0 if not it will make it the size of artists array

@Override public int getItemCount() {      int a ;      if(artists != null && !artists.isEmpty()) {          a = artists.size();     }     else {          a = 0;       }     return a; } 


回答9:

This is really a simple error you are getting, there in no need of doing any codes in this. This error occurs due to the wrong layout file used by the activity. By IDE i automatically created a layout v21 of a layout which became a default layout of the activity. all codes I did in the old layout file and new one was only having few xml codes, which led to that error.

Solution: Copy all codes of old layout and paste in layout v 21



回答10:

These Lines must be in OnCreate:

mmAdapter = new Adapter(msgList); mrecyclerView.setAdapter(mmAdapter); 


回答11:

In my case, I was setting the adapter inside onLocationChanged() callback AND debugging in the emulator. Since it didn't detected a location change it never fired. When I set them manually in the Extended controls of the emulator it worked as expected.



回答12:

I have solved this error. You just need to add layout manager and add the empty adapter.

Like this code:

myRecyclerView.setLayoutManager(...//your layout manager);         myRecyclerView.setAdapter(new RecyclerView.Adapter() {             @Override             public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {                 return null;             }              @Override             public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {              }              @Override             public int getItemCount() {                 return 0;             }         }); //other code's  // and for change you can use if(mrecyclerview.getadapter != speacialadapter){ //replice your adapter //} 


回答13:

ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists); recyclerView = (RecyclerView) findViewById(R.id.cardList); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); 

Just replace above code with this and it should work. What you did wrong is you called setAdapter(adapter) before calling layout manager.



回答14:

In my situation it was a forgotten component which locates in ViewHolder class but it wasn't located in layout file



回答15:

I had the same problem and realized I was setting both the LayoutManager and adapter after retrieving the data from my source instead of setting the two in the onCreate method.

salesAdapter = new SalesAdapter(this,ordersList);         salesView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));         salesView.setAdapter(salesAdapter); 

Then notified the adapter on data change

               //get the Orders                 Orders orders;                 JSONArray ordersArray = jObj.getJSONArray("orders");                     for (int i = 0; i 


回答16:

This issue is because you are not adding any LayoutManager for your RecyclerView.

Another reason is because you are calling this code in a NonUIThread. Make sure to call this call in the UIThread.

The solution is only you have to add a LayoutManager for the RecyclerView before you setAdapter in the UI Thread.

recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 


回答17:

This happens because the actual inflated layout is different from that which is being referred by you while finding the recyclerView. By default when you create the fragment, the onCreateView method appears as follows: return inflater.inflate(R.layout.,container.false);

Instead of that, separately create the view and use that to refer to recyclerView View view= inflater.inflate(R.layout.,container.false); recyclerview=view.findViewById(R.id.); return view;



回答18:

My problem was my recycler view looked like this

        

When it should have looked like this

       


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!