刚刚在我的代码中实现了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添加任何LayoutManager。

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

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

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

其他回答

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

我做了一个私有方法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;}

    }

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

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

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

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

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

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

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