recyclerview No adapter attached; skipping layout
Asked Answered
C

38

327

Just implemented RecyclerView in my code, replacing ListView.

Everything works fine. The data is displayed.

But error messages are being logged:

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

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

for the following 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 RecyclerView. So why do I keep getting this error?

I have read other questions related to the same problem but none of them help.

Charioteer answered 19/3, 2015 at 10:9 Comment(13)
is artists empty ? what happens you switch setAdapter and setLayoutManager?Belayneh
You have to use a RecyclerView.AdapterUniformize
@BlackBelt nothing different happens when i switch setAdapter and setLayoutManager (i see all the artists and the error message in logcat as well) and as for is artists empty?, I'm calling this code from retrofit's success after it finishes downloading all the data. So i don't think artists is empty (as i can see all the artists!)Charioteer
@FreeFlyer94 ArtistArrayAdapter is a class extending RecyclerView.AdapterCharioteer
You might be seeing it if you are not initializing recyclerview as soon as it is attached to the window.Fitzger
@Fitzger I'm waiting for retrofit to download data and after it's finished the given code runs!Charioteer
ok then it makes sense because recycler view is laid out before u have data thus it prints the error.Fitzger
@Fitzger but after successful download I see the data... so can the error be ignored? or how do i fix it?Charioteer
you can ignore it. or if u want to get rid of it, set an empty adapter when you initialize RecyclerView.Fitzger
How serious is this error? Can this be ignored? Anyways I use setAdapter(null) to avoid the error.Launch
Reference this article makecodesimpleandeasy.blogspot.com/p/…Jordaens
i am puzzled by the answers suggesting binding the adapter in onCreate. at that point, the view hasn't been created, so findViewById isn't available. nor does Android Studio allow me to put setContentView at that point. it seems i MUST wait until onViewCreated to bind the adapter. what am I missing here?Drennan
i kept same thing but order was wrong in my case, i was keeping setAdapter lastly making causeBujumbura
A
287

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 );
Alduino answered 1/6, 2015 at 19:42 Comment(12)
But I need to download the data first then display!Charioteer
Just set an empty adapter first, update it as soon as you have the data (this is what works for me).Alduino
That's what yigit suggested!Charioteer
More or less, in my app I'm working with the Google Drive API. I perform the initialization within the onConnected method using an empty adapter, when I receive the ResultCallback I'm adding the data into the adapter.Alduino
@Charioteer You should set a empty adapter first,and then the data is downloaded,call mAdapter.notifyDataSetChanged(),that is worked for me.Humanitarian
This solution isn't helping me. In fact no solution in this thread is helping me. Can someone please look into my case hereBlinny
One liner: list.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)Lemal
I had that code in a function that was never colled from onViewCreatedTracietracing
This solution works for me. I forgot 'llm.setOrientation(LinearLayoutManager.VERTICAL);'. After adding this, the skipping layout error is gone. ThanksDanialdaniala
@Humanitarian How can I do that? Can you look at my question at https://mcmap.net/q/98564/-recyclerview-no-adapter-attached-skipping-layout/13935956Deanedeaner
@Alduino How can I do that? Can you look at my question at https://mcmap.net/q/98564/-recyclerview-no-adapter-attached-skipping-layout/13935956Deanedeaner
How much different is the Kotlin version of this code?Elementary
G
53

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.

Gerrygerrymander answered 1/7, 2015 at 16:21 Comment(3)
my getItemCount() looks like this public int getItemCount() { return artists == null ? 0 : artists.size(); } and the LayoutInflater View itemView = LayoutInflater. from(parent.getContext()).inflate(R.layout.fragment_listitem, parent, false); Charioteer
this works for me: @Override public int getItemCount() { return artists.size(); }Wince
Oh God. That moment when you come back to SO, and see an answer that you already upvoted, and realize that you were doing the same stupid thing. Thank again @Micro.Soothfast
H
25

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 {
Howard answered 6/8, 2015 at 8:3 Comment(1)
I'm sorry, but in my version I cannot use "this.findViewById()" in onCreate to define the RecyclerView. it is simple forbidden by Android Studio (cannot resolve). nor does it allow me to use setContentView. this answer is old so I assume this is a version issue. i am forced to wait to set the adapter until onViewCreated, when i can use findViewById to define the RecyclerView. I must be missing something!Drennan
G
16

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)

Glaudia answered 4/2, 2016 at 15:28 Comment(1)
setting ScrollView.fillViewport to true solved my problem, thanks a bunchDetraction
W
12

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<SampleHolder> {
    @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.

Wadai answered 25/8, 2015 at 9:11 Comment(2)
Hi , please i have the same error and i don't know where i should check if the recycler is not ready tto passe an empty adapter) and where i can check if the recycler is ready(to pass the adapter with data)Cheryllches
This is great, here's a drag'n drop style gist gist.github.com/anonymous/bcf027945f08f40601090c55d6048e21Demonize
A
10

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();
Allocate answered 4/8, 2015 at 8:9 Comment(1)
make sure you set data in customadapter constructor within onCreate methodCattle
C
6

Check if you have missed to call this method in your adapter

@Override
public int getItemCount() {
    return list.size();
}
Cinematograph answered 16/10, 2018 at 18:48 Comment(0)
C
6

In Kotlin we had this weird illogical issue.

This didn't work:

 mBinding.serviceProviderCertificates.apply {
            adapter = adapter
            layoutManager =  LinearLayoutManager(activity)
        }

While this worked:

mBinding.serviceProviderCertificates.adapter = adapter
mBinding.serviceProviderCertificates.layoutManager = LinearLayoutManager(activity)

Once I get more after work hours, I will share more insights.

Cleromancy answered 28/2, 2020 at 15:56 Comment(3)
I'm shocked, that's actual issue I had. Very strange indeed.Jeannettejeannie
Wow, weird. Even weirder is that it works if you replace apply with let , also or run.Tatiana
Yes its working for me!Cowpea
B
5

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.

Bills answered 16/1, 2017 at 9:0 Comment(0)
S
4
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.

Stefaniastefanie answered 8/8, 2015 at 12:14 Comment(0)
P
4

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;
}
Presbyter answered 19/2, 2017 at 7:8 Comment(0)
Z
4

For those who use the RecyclerView within a fragment and inflate it from other views: when inflating the whole fragment view, make sure that you bind the RecyclerView to its root view.

I was connecting and doing everything for the adapter correctly, but I never did the binding. This answer by @Prateek Agarwal has it all for me, but here is more elaboration.

Kotlin

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val rootView =  inflater?.inflate(R.layout.fragment_layout, container, false)
    recyclerView = rootView?.findViewById(R.id.recycler_view_id)
    // rest of my stuff
    recyclerView?.setHasFixedSize(true)
    recyclerView?.layoutManager = viewManager
    recyclerView?.adapter = viewAdapter
    // return the root view
    return rootView
}

Java

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView= inflater.inflate(R.layout.fragment_layout,container,false);
    recyclerview= rootView.findViewById(R.id.recycler_view_id);
    return rootView;
}
Zaragoza answered 10/5, 2018 at 12:31 Comment(0)
O
4

In my layout xml file, the bottom line with layoutManager was missing. The error disappeared after I added it.

   <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view_chat"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:layoutManager="LinearLayoutManager"/>
Onceover answered 3/4, 2021 at 5:44 Comment(0)
E
3

These Lines must be in OnCreate:

mmAdapter = new Adapter(msgList);
mrecyclerView.setAdapter(mmAdapter);
Ethiopic answered 1/3, 2017 at 9:51 Comment(0)
R
3

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.<related layout>,container.false);

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

Rother answered 22/1, 2018 at 20:3 Comment(1)
I spent hours on this. This is the response that worked in my case. binding.recyclerView was not the correct reference. Instead I got the RecyclerView like this: recyclerView = root.findViewById(R.id.m_recyler_view)Whaling
F
2

First initialize the adapter

public void initializeComments(){
    comments = new ArrayList<>();

    comments_myRecyclerView = (RecyclerView) findViewById(R.id.comments_recycler);
    comments_mLayoutManager = new LinearLayoutManager(myContext);
    comments_myRecyclerView.setLayoutManager(comments_mLayoutManager);

    updateComments();
    getCommentsData();

}

public void updateComments(){

    comments_mAdapter = new CommentsAdapter(comments, myContext);
    comments_myRecyclerView.setAdapter(comments_mAdapter);
}

When ever there is a change in the dataset set, just call the updateComments method.

Fleabitten answered 19/4, 2018 at 6:38 Comment(0)
L
2

I had this error, and I tried to fix for a while until I found the solution.

I made a private method buildRecyclerView, and I called it twice, first on onCreateView and then after my callback (in which I fetch data from an API). This is my method buildRecyclerView in my Fragment:

private void buildRecyclerView(View v) {
        mRecyclerView = v.findViewById(R.id.recycler_view_loan);
        mLayoutManager = new LinearLayoutManager(getActivity());
        ((LinearLayoutManager) mLayoutManager).setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new LoanAdapter(mExampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
}

Besides, I have to modify the method get-Item-Count in my adapter, because On on-Create-View the list is null and it through an error. So, my get-Item-Count is the following:

@Override
    public int getItemCount() {
        try {
            return mLoanList.size();
        } catch (Exception ex){return 0;}

    }
Leprosy answered 30/4, 2020 at 10:54 Comment(0)
S
2

In my case, I just added layout manager to the recyclerview and got fixed. In the recyclerview in your XML file just add app:layoutManager

 <androidx.recyclerview.widget.RecyclerView
     android:id="@+id/recyclerViewData"
     .
     .
     android:orientation="vertical"
     app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
     .
     ./>
Stucco answered 15/2, 2023 at 12:39 Comment(0)
L
1

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
//}
Ligulate answered 11/3, 2016 at 14:41 Comment(1)
i do it for beginner usersLigulate
G
1

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

Gustafsson answered 31/10, 2016 at 12:25 Comment(0)
C
1

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.

Comus answered 24/5, 2017 at 19:51 Comment(0)
P
1

Just add the following to RecyclerView

app:layoutManager="android.support.v7.widget.LinearLayoutManager"

Example:

   <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.v7.widget.RecyclerView>
Pew answered 30/11, 2018 at 13:37 Comment(0)
K
1

In case you're getting still error while using ViewBinding, make sure you're using the binding to return the inflated view ie

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return fragmentBinding.getRoot();
}
Keeler answered 16/6, 2021 at 18:3 Comment(0)
G
0

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

Grammatical answered 29/9, 2017 at 23:8 Comment(0)
M
0

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 < ordersArray.length() ; i++) {
                        JSONObject orderItem = ordersArray.getJSONObject(i);
                        //populate the Order model

                        orders = new Orders(
                                orderItem.getString("order_id"),
                                orderItem.getString("customer"),
                                orderItem.getString("date_added"),
                                orderItem.getString("total"));
                        ordersList.add(i,orders);
                        salesAdapter.notifyDataSetChanged();
                    }
Maragretmarala answered 22/10, 2017 at 11:39 Comment(0)
K
0

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()));
Korfonta answered 26/11, 2017 at 14:52 Comment(1)
If you need to delay setting LayoutManager you should do this from your callback method: recyclerView.post(() -> { recyclerView.setLayoutManager(mGridLayoutManager); recyclerView.addItemDecoration(new ItemOffsetDecoration(itemGap)); });Barge
A
0

Solved by setting the initialized empty list and adapter at the bottom and calling notifyDataSetChanged when results are fetched.

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    recyclerviewItems.setLayoutManager(linearLayoutManager);
    someAdapter = new SomeAdapter(getContext(),feedList);
    recyclerviewItems.setAdapter(someAdapter);
Ashton answered 17/4, 2018 at 23:33 Comment(0)
D
0

I lost 16 minutes of my life with this issue, so I'll just admit to this incredibly embarrassing mistake that I was making- I'm using Butterknife and I bind the view in onCreateView in this fragment.

It took a long time to figure out why I had no layoutmanager - but obviously the views are injected so they won't actually be null, so the the recycler will never be null .. whoops!

@BindView(R.id.recycler_view)
RecyclerView recyclerView;

    @Override
public View onCreateView(......) {
    View v = ...;
    ButterKnife.bind(this, v);
    setUpRecycler()
 }

public void setUpRecycler(Data data)
   if (recyclerView == null) {
 /*very silly because this will never happen*/
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
       //more setup
       //...
    }
    recyclerView.setAdapter(new XAdapter(data));
}

If you are getting an issue like this trace your view and use something like uiautomatorviewer

Diandre answered 4/6, 2018 at 16:26 Comment(0)
I
0

In my case it happened cause i embedded a RecyclerView in a LinearLayout.

I previously had a layout file only containing one root RecyclerView as follows

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@layout/fragment_products"

    android:name="Products.ProductsFragment"
    app:layoutManager="LinearLayoutManager"
    tools:context=".Products.ProductsFragment"

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"/>

I believe the problem is within the 3 lines separated. Anyway, I think its a simple problem, ill be working on it tomorrow; thought i should write what i found before forgetting about this thread.

Inapproachable answered 29/11, 2018 at 16:32 Comment(0)
C
0

Adding yet another answer since I came across this thread googling the error. I was trying to initialize a PreferenceFragmentCompat but I forgot to inflate the preference XML in onCreatePreferences like this:

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.fragment_settings, null)
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        // Missing this line here:
        //   setPreferencesFromResource(R.xml.settings, rootKey)
    }
}

The error was a mystery until I realized that PreferenceFragmentCompat must be using a RecyclerView internally.

Cyclorama answered 12/2, 2019 at 2:12 Comment(0)
P
0

// It happens when you are not setting the adapter during the creation phase: call notifyDataSetChanged() when api response is getting Its Working

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);


        magazineAdapter = new MagazineAdapter(getContext(), null, this );
        newClipRecyclerView.setAdapter(magazineAdapter);
        magazineAdapter.notifyDataSetChanged();

       APICall();
}

public void APICall() {
    if(Response.isSuccessfull()){
    mRecyclerView.setAdapter(mAdapter);
   }
}
Just move setting the adapter into onCreate with an empty data and when you have the data call:

mAdapter.notifyDataSetChanged();
Pavis answered 2/7, 2019 at 12:35 Comment(0)
C
0

I was getting same issue, I did every thing correct excepted in xml file:

  • step first 1: initialize recyclerview & List & Adaptor:

    RecyclerView recyclerview;
    List<ModelName> list;
    ProvideBidAdaptor adaptor;
    
  • step 2: bind it in onCreate-

    recyclerview = findByViewId(R.id.providerBidRV);
    recyclerview.setLayoutManager(new LinearLayoutManager(this));
    recyclerview.setHasFixedSize(true);
    list = new ArrayList<>();
    
  • step 3: Where you getting response or list - add list- (i am getting from response)>

    responseList.addAll(response.getResponse());
    adaptor = new ProvideBidAdaptor(this, responseList);
    binding.detailsRV.setAdapter(adaptor);
    

Here is my xml file where i implement RecyclerView: I was forget orientation in LinearLayout, after this correction- RecyclerView attached.

<?xml version="1.0" encoding="utf-8"?>
    <layout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".activities.Bidding.ProvideBid">

        <include
            android:id="@+id/toolbar"
            layout="@layout/app_toolbar"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/providerBidRV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</layout> 

Here is Adaptor:

    public class ProvideBidAdaptor extends RecyclerView.Adapter<ProvideBidAdaptor.ViewHolder> {
    Context context;
    List<Response> responseList;
    DateTime dateTimeInstance = new DateTime();

    public ProvideBidAdaptor(Context context, List<Response> responseList) {
        this.context = context;
        this.responseList = responseList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.geo_presence_item_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Response detailsResponse = responseList.get(position);
        if (!detailsResponse.getUserId().isEmpty()) {

            holder.day.setText(detailsResponse.getDay());
            holder.locationType.setText(detailsResponse.getLocationType());

        }

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView date,locationType;
        CardView provideBidCV;
        LinearLayout dayLLayout,locationTypeLLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date_value);
            day = itemView.findViewById(R.id.day_value);
            locationType = itemView.findViewById(R.id.locationType_value);

            locationTypeLLayout = itemView.findViewById(R.id.locationTypeLLayout);

        }
    }
}
Croce answered 17/1, 2020 at 12:4 Comment(0)
B
0
 override fun onCreateView(...  ): View? 
   {
        val rootview = inflater.inflate(R.layout.fragment_menu, 
        container,false)

        val recycler_menu_list = 
         rootview.findViewById(R.id.recycler_menu_list) as RecyclerView

        recycler_menu_list.layoutManager = LinearLayoutManager(activity)

        val data =  ...

        foodCardAdapter = FoodCardAdapter()

        recycler_menu_list.adapter=foodCardAdapter

        foodCardAdapter.submitList(data)      

       //make sure that the return view is one declared above up and is 
       //not the default 'inflater.inflate(R.layout.fragment_menu,         
       //container,false)' created by the ide

        return rootview    
    }
Bionomics answered 10/10, 2021 at 16:15 Comment(0)
A
0

Make sure to set attachToRoot attribute to false in the inflate() method of your databinding or whatever you're using to display views like so:

inflate(layoutInflater, parent, false)

Accident answered 8/1, 2022 at 18:45 Comment(0)
B
0

In my case I was getting this error because I forgot to attach the layout manager in my XML layout file.

It was:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/recycler_view_to_do"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:listitem="@layout/item_generic"
    tools:context=".ToDoActivity">
</androidx.recyclerview.widget.RecyclerView>

By adding the attribute app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" (and the corresponding namespace xmlns:app="http://schemas.android.com/apk/res-auto" which was auto-added by Android Studio) my XML layout file became:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:id="@+id/recycler_view_to_do"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

    tools:listitem="@layout/item_generic"
    tools:context=".ToDoActivity">
</androidx.recyclerview.widget.RecyclerView>

and the error disappeared.

Borreri answered 12/2, 2023 at 18:1 Comment(0)
S
-1

In your RecyclerView adapter class, for example MyRecyclerViewAdapter, make a constructor with the following params.

MyRecyclerViewAdapter(Context context, List<String> data) {
    this.mInflater = LayoutInflater.from(context); // <-- This is the line most people include me miss
    this.mData = data;
}

mData is the data that you'll pass to the adapter. It is optional if you have no data to be passed. mInflater is the LayoutInflater object that you have created and you use in the OnCreateViewHolder function of the adapter.

After this, you attach the adapter in the MainActivity or wherever you want to on the main/UI thread properly like

MyRecyclerViewAdapter recyclerAdapter;
OurDataStuff mData;

    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Like this:

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerAdapter = new RecyclerAdapter(this, mData); //this, is the context. mData is the data you want to pass if you have any
        recyclerView.setAdapter(recyclerAdapter);
    }
   ....
Sirocco answered 22/3, 2018 at 19:16 Comment(0)
C
-1

It worked for me.

  1. Do this if you are using the emulator.

android:usesCleartextTraffic="true"

  1. If your cardview xml design is like this,

android.support.v7.widget.CardView

Replace cardview xml design with android x. Must be such;

androidx.cardview.widget.CardView

  1. If your recyclerview xml design is like this,

android.support.v7.widget.RecyclerView

Replace cardview xml design with android x. Must be such;

androidx.recyclerview.widget.RecyclerView

Califate answered 22/10, 2019 at 13:22 Comment(1)
Nothing about this answer makes senseArnaud
B
-1

Just put code inside onCreate() method

/* Like This*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    layoutManager = new LinearLayoutManager(YourActivity.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    mAdapter = new YourAdapter(YourModelClassObject);
    recyclerView.setAdapter(mAdapter);
}
Brie answered 4/4, 2020 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.