android - recyclerview No adapter attached; skipping layout

ID : 10302

viewed : 29

Tags : androidandroid-recyclerviewandroid

Top 5 Answer for android - recyclerview No adapter attached; skipping layout

vote vote

100

Can you make sure that you are calling these statements from the "main" thread outside of a delayed asynchronous callback (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 ); 
vote vote

85

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:

<android.support.v7.widget.RecyclerView     android:id="@+id/RecyclerView"     android:layout_width="match_parent"     android:layout_height="match_parent"     /> 

NOT like this:

<view     android:id="@+id/RecyclerView"     class="android.support.v7.widget.RecyclerView"     android:layout_width="match_parent"     android:layout_height="match_parent" /> 

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

vote vote

80

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 { 
vote vote

65

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 .

(Can be followed for android.support.v4.widget.NestedScrollView as well)

vote vote

55

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(); 

Top 3 video Explaining android - recyclerview No adapter attached; skipping layout

Related QUESTION?