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

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


当前回答

确保在你的数据绑定或任何你用来显示视图的方法的充气()方法中将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" />

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

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

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

我得到了同样的问题,我做的每件事都是正确的,除了在xml文件:

step first 1: initialize recyclerview & List & Adaptor: RecyclerView recyclerview; List<ModelName> list; ProvideBidAdaptor adaptor; step 2: bind it in onCreate- recyclerview = findByViewId(R.id.providerBidRV); recyclerview.setLayoutManager(new LinearLayoutManager(this)); recyclerview.setHasFixedSize(true); list = new ArrayList<>(); step 3: Where you getting response or list - add list- (i am getting from response)> responseList.addAll(response.getResponse()); adaptor = new ProvideBidAdaptor(this, responseList); binding.detailsRV.setAdapter(adaptor);

这是我的xml文件,我实现RecyclerView: 我忘记了线性布局的方向,经过这个修正- RecyclerView附加。

<?xml version="1.0" encoding="utf-8"?>
    <layout>
    <LinearLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".activities.Bidding.ProvideBid">

        <include
            android:id="@+id/toolbar"
            layout="@layout/app_toolbar"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/providerBidRV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</layout> 

这是适配器:

    public class ProvideBidAdaptor extends RecyclerView.Adapter<ProvideBidAdaptor.ViewHolder> {
    Context context;
    List<Response> responseList;
    DateTime dateTimeInstance = new DateTime();

    public ProvideBidAdaptor(Context context, List<Response> responseList) {
        this.context = context;
        this.responseList = responseList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.geo_presence_item_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Response detailsResponse = responseList.get(position);
        if (!detailsResponse.getUserId().isEmpty()) {

            holder.day.setText(detailsResponse.getDay());
            holder.locationType.setText(detailsResponse.getLocationType());

        }

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView date,locationType;
        CardView provideBidCV;
        LinearLayout dayLLayout,locationTypeLLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date_value);
            day = itemView.findViewById(R.id.day_value);
            locationType = itemView.findViewById(R.id.locationType_value);

            locationTypeLLayout = itemView.findViewById(R.id.locationTypeLLayout);

        }
    }
}

对于那些在片段中使用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;
}

我有和你一样的情况,显示正常,但是定位器出现错误。 这就是我的解决方案: (1)在CREATE()上初始化RecyclerView并绑定适配器

RecyclerView mRecycler = (RecyclerView) this.findViewById(R.id.yourid);
mRecycler.setAdapter(adapter);

(2)当你得到数据时调用notifyDataStateChanged

adapter.notifyDataStateChanged();

在recyclerView的源代码中,有另一个线程来检查数据的状态。

public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.mObserver = new RecyclerView.RecyclerViewDataObserver(null);
    this.mRecycler = new RecyclerView.Recycler();
    this.mUpdateChildViewsRunnable = new Runnable() {
        public void run() {
            if(RecyclerView.this.mFirstLayoutComplete) {
                if(RecyclerView.this.mDataSetHasChangedAfterLayout) {
                    TraceCompat.beginSection("RV FullInvalidate");
                    RecyclerView.this.dispatchLayout();
                    TraceCompat.endSection();
                } else if(RecyclerView.this.mAdapterHelper.hasPendingUpdates()) {
                    TraceCompat.beginSection("RV PartialInvalidate");
                    RecyclerView.this.eatRequestLayout();
                    RecyclerView.this.mAdapterHelper.preProcess();
                    if(!RecyclerView.this.mLayoutRequestEaten) {
                        RecyclerView.this.rebindUpdatedViewHolders();
                    }

                    RecyclerView.this.resumeRequestLayout(true);
                    TraceCompat.endSection();
                }

            }
        }
    };

在dispatchLayout()中,我们可以发现其中有错误:

void dispatchLayout() {
    if(this.mAdapter == null) {
        Log.e("RecyclerView", "No adapter attached; skipping layout");
    } else if(this.mLayout == null) {
        Log.e("RecyclerView", "No layout manager attached; skipping layout");
    } else {