recyclerview No adapter attached; skipping layout

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

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

提交回复
热议问题