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

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


当前回答

在我的情况下,它是一个被遗忘的组件,位于ViewHolder类,但它不在布局文件中

其他回答

你能确保你在延迟异步回调(例如在onCreate()方法内部)之外的“主线程”调用这些语句吗? 只要我从“延迟”方法调用相同的语句。在我的例子中是ResultCallback,我得到了相同的消息。

在我的片段中,从ResultCallback方法中调用下面的代码会产生相同的消息。在将代码移动到我的应用程序中的onConnected()方法后,消息消失了…

LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter( adapter );

这些行必须在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>

我有这个错误,我试着修复了一段时间,直到我找到解决方案。

我做了一个私有方法buildRecyclerView,我调用它两次,第一次在onCreateView,然后在我的回调(其中我从API获取数据)。这是我的方法buildRecyclerView在我的片段:

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

此外,我必须修改我的适配器中的get-Item-Count方法,因为On - On - create - view列表是空的,它通过一个错误。所以,我的get-Item-Count如下所示:

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

    }

解决方法是在底部设置初始化的空列表和适配器,并在获取结果时调用notifyDataSetChanged。

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    recyclerviewItems.setLayoutManager(linearLayoutManager);
    someAdapter = new SomeAdapter(getContext(),feedList);
    recyclerviewItems.setAdapter(someAdapter);