recyclerview No adapter attached; skipping layout

后端 未结 30 3150
走了就别回头了
走了就别回头了 2020-11-21 04:51

Just implemented RecyclerView in my code, replacing ListView.

Everything works fine. The data is displayed.

But error messages are

30条回答
  •  情歌与酒
    2020-11-21 05:30

    I have the same situation with you, display is ok, but error appear in the locat. That's my solution: (1) Initialize the RecyclerView & bind adapter ON CREATE()

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

    (2) call notifyDataStateChanged when you get the data

    adapter.notifyDataStateChanged();
    

    In the recyclerView's source code, there is other thread to check the state of data.

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

    In the dispatchLayout(), we can find there is the error in it:

    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 {
    

提交回复
热议问题