刚刚在我的代码中实现了RecyclerView,取代了ListView。

一切都很好。显示数据。

但是错误消息正在被记录:

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

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

对于以下代码:

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

正如您所看到的,我已经为RecyclerView附加了一个适配器。 为什么我总是得到这个错误呢?

我读过与这个问题相关的其他问题,但没有一个有用。


当前回答

在我的情况下,它发生了,因为我嵌入了一个RecyclerView在线性布局。

我以前有一个布局文件只包含一个根RecyclerView如下

<?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"/>

我相信问题就在这三行中。不管怎样,我想这是个简单的问题,我明天再来解决。我想我应该在忘记这个帖子之前写下我发现的东西。

其他回答

这些行必须在OnCreate中:

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

我遇到了同样的问题,并意识到我在从源检索数据后同时设置了LayoutManager和适配器,而不是在onCreate方法中设置这两个。

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

然后将数据更改通知适配器

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

在Kotlin,我们遇到了一个奇怪的不合逻辑的问题。

这并没有起作用:

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

虽然这是有效的:

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

工作之余,我也会分享更多的真知灼见。

首先初始化适配器

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

当数据集中发生变化时,只需调用updateComments方法。

我也犯了同样的错误,我修复了它如果你像我一样在等待数据,使用改造或类似的东西

放在Oncreate之前

private ArtistArrayAdapter adapter;
private RecyclerView recyclerView;

把它们放在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);

当你收到数据时放

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

现在进入ArtistArrayAdapter类,执行这个操作它会做的是如果数组为空或为空它会让GetItemCount返回0如果不是它会让它成为艺术家数组的大小

@Override
public int getItemCount() {

    int a ;

    if(artists != null && !artists.isEmpty()) {

        a = artists.size();
    }
    else {

        a = 0;

     }

   return a;
}