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

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


当前回答

在我的例子中,我在模拟器中设置onLocationChanged()回调和调试适配器。因为它没有检测到位置变化,所以没有开火。当我在模拟器的扩展控件中手动设置它们时,它按预期工作。

其他回答

这是一个很简单的错误,不需要做任何代码。 此错误是由于活动使用了错误的布局文件。通过IDE,我自动创建了一个布局v21的布局,成为活动的默认布局。 我在旧布局文件和新文件中所做的所有代码只有少数xml代码,这导致了这个错误。

解决方案:复制旧布局的所有代码,并粘贴在布局v 21

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

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

这并没有起作用:

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

虽然这是有效的:

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

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

确保在你的数据绑定或任何你用来显示视图的方法的充气()方法中将attachtorroot属性设置为false:

充气(layoutInflater, parent, false)

我得到了相同的两个错误消息,直到我在我的代码中修复了两个东西:

默认情况下,当你在RecyclerView中实现方法时。它生成的适配器:

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

确保你更新了你的代码,让它说:

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

显然,如果你的项目中没有项目,那么屏幕上显示的东西就会是零。

(2)我没有这样做,如上面的答案所示:CardView layout_width="match_parent"不匹配父RecyclerView宽度

//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)编辑:奖金: 还要确保你像这样设置你的RecyclerView:

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

不是这样的:

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

我看过一些使用后一种方法的教程。当它工作时,我认为它也会产生这个错误。