recyclerview No adapter attached; skipping layout

后端 未结 30 3145
走了就别回头了
走了就别回头了 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:12

    It happens when you are not setting the adapter during the creation phase:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        ....
    }
    
    public void onResume() {
        super.onResume();
        mRecyclerView.setAdapter(mAdapter);
        ....
    }
    

    Just move setting the adapter into onCreate with an empty data and when you have the data call:

    mAdapter.notifyDataSetChanged();
    
    0 讨论(0)
  • 2020-11-21 05:12

    I had this error, and I tried to fix for a while until I found the solution.

    I made a private method buildRecyclerView, and I called it twice, first on onCreateView and then after my callback (in which I fetch data from an API). This is my method buildRecyclerView in my Fragment:

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

    Besides, I have to modify the method get-Item-Count in my adapter, because On on-Create-View the list is null and it through an error. So, my get-Item-Count is the following:

    @Override
        public int getItemCount() {
            try {
                return mLoanList.size();
            } catch (Exception ex){return 0;}
    
        }
    
    0 讨论(0)
  • 2020-11-21 05:12

    This issue is because you are not adding any LayoutManager for your RecyclerView.

    Another reason is because you are calling this code in a NonUIThread. Make sure to call this call in the UIThread.

    The solution is only you have to add a LayoutManager for the RecyclerView before you setAdapter in the UI Thread.

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    
    0 讨论(0)
  • 2020-11-21 05:14
    ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists);
    recyclerView = (RecyclerView) findViewById(R.id.cardList);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    

    Just replace above code with this and it should work. What you did wrong is you called setAdapter(adapter) before calling layout manager.

    0 讨论(0)
  • 2020-11-21 05:16

    I was getting the same two error messages until I fixed two things in my code:

    (1) By default, when you implement methods in the RecyclerView.Adapter it generates:

    @Override
    public int getItemCount() {
        return 0;
    }
    

    Make sure you update your code so it says:

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

    Obviously if you have zero items in your items then you will get zero things displayed on the screen.

    (2) I was not doing this as shown in the top answer: CardView layout_width="match_parent" does not match parent RecyclerView width

    //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) EDIT: BONUS: Also make sure you set up your RecyclerView like this:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    

    NOT like this:

    <view
        android:id="@+id/RecyclerView"
        class="android.support.v7.widget.RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    I have seen some tutorials using the latter method. While it works I think it generates this error too.

    0 讨论(0)
  • 2020-11-21 05:16

    Solved by setting the initialized empty list and adapter at the bottom and calling notifyDataSetChanged when results are fetched.

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerviewItems.setLayoutManager(linearLayoutManager);
        someAdapter = new SomeAdapter(getContext(),feedList);
        recyclerviewItems.setAdapter(someAdapter);
    
    0 讨论(0)
提交回复
热议问题