刚刚在我的代码中实现了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附加了一个适配器。 为什么我总是得到这个错误呢?

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


当前回答

在我的布局xml文件中,缺少了带有layoutManager的底线。我添加后错误消失了。

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

其他回答

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

只要用这个替换上面的代码,它就应该工作了。你所做的错误是在调用布局管理器之前调用了setAdapter(适配器)。

在您的RecyclerView适配器类中,例如MyRecyclerViewAdapter,使用以下参数创建一个构造函数。

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

mData是传递给适配器的数据。如果没有要传递的数据,则该选项是可选的。 mInflater是你创建的LayoutInflater对象,你在适配器的OnCreateViewHolder函数中使用。

在此之后,您可以将适配器附加到MainActivity或主/UI线程上您想要的任何地方

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

在我的情况下,它发生了,因为我嵌入了一个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"/>

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

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

这并没有起作用:

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

虽然这是有效的:

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

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

如果你在使用ViewBinding时得到still错误,请确保你使用绑定来返回膨胀的视图

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return fragmentBinding.getRoot();
}