刚刚在我的代码中实现了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时引用的布局不同。默认情况下,当你创建片段时,onCreateView方法如下所示: 返回inflater.inflate(出来。< >相关布局,container.false);

相反,单独创建视图并使用它引用recyclerView 视图视图= inflater. inflation (r.p ayler .layout。< >相关布局,container.false); recyclerview = view.findViewById (R.id。< recyclerView ID >); 返回视图;

其他回答

首先初始化适配器

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方法。

这对我很管用。

如果您正在使用模拟器,请执行此操作。

android:usesCleartextTraffic=“true”

如果你的cardviewxml设计是这样的,

android.support.v7.widget.CardView

用android x替换cardviewxml设计。 必须如此;

androidx.cardview.widget.CardView

如果你的recyclerview xml设计是这样的,

android.support.v7.widget.RecyclerView

用android x替换cardviewxml设计。 必须如此;

androidx.recyclerview.widget.RecyclerView

这些行必须在OnCreate中:

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

只需将以下内容添加到RecyclerView

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

例子:

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

我因为这个问题失去了16分钟的生命,所以我要承认这个令人难以置信的尴尬的错误,我正在使用Butterknife,我在这个片段中绑定onCreateView中的视图。

花了很长时间才弄清楚为什么我没有布局管理器-但显然,视图是注入的,所以它们实际上不会为空,所以回收器永远不会为空。哎呀!

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

如果你遇到这样的问题,跟踪你的视图,使用uiautomatorviewer之类的东西