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

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


当前回答

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

放在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;
}

其他回答

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

默认情况下,当你在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" />

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

这个问题是因为您没有为您的RecyclerView添加任何LayoutManager。

另一个原因是因为你在NonUIThread中调用了这个代码。确保在UIThread中调用这个调用。

解决方案是你必须在UI线程中setAdapter之前为RecyclerView添加一个LayoutManager。

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

对于那些在片段中使用RecyclerView并从其他视图中扩展它的人:当扩展整个片段视图时,确保将RecyclerView绑定到它的根视图。

我为适配器正确地连接和做了所有事情,但我从未进行绑定。 @Prateek Agarwal的回答已经为我提供了一切,但这里有更详细的说明。

科特林

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val rootView =  inflater?.inflate(R.layout.fragment_layout, container, false)
    recyclerView = rootView?.findViewById(R.id.recycler_view_id)
    // rest of my stuff
    recyclerView?.setHasFixedSize(true)
    recyclerView?.layoutManager = viewManager
    recyclerView?.adapter = viewAdapter
    // return the root view
    return rootView
}

Java

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView= inflater.inflate(R.layout.fragment_layout,container,false);
    recyclerview= rootView.findViewById(R.id.recycler_view_id);
    return rootView;
}

只需将代码放入onCreate()方法中

/* Like This*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    layoutManager = new LinearLayoutManager(YourActivity.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    mAdapter = new YourAdapter(YourModelClassObject);
    recyclerView.setAdapter(mAdapter);
}

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

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