"RecyclerView: No Adapter attached; skipping layout" for recyclerview in fragment [duplicate]
Asked Answered
F

3

10

I'm getting this error but I don't know what is causing it...perhaps something to do with the fact that this is being initialized in a fragment and not in the activity itself. Edit: included StatsAdapter code

public class StatsFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private RecyclerView.Adapter mAdapter;
    private String[] myDataset = new String[]{"hello", "world", "yolo"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_stats, parent, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new StatsAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        return v;
    }
}



public class StatsAdapter extends RecyclerView.Adapter<StatsAdapter.ViewHolder> {
    private String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.title);
        }
    }

    public StatsAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public StatsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stats, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}
Fibrillation answered 17/6, 2015 at 15:11 Comment(8)
Create the adapter and the "non-view" related objects in onCreate. Then use your adapter for the recyclerview.Cinda
@Jared Burrows, that doesn't change the outcome despite it being correct.Ossian
@Ossian Post your answer or correct mine.Cinda
@hshanguan, this specific error message appears when your adapter has not been set (or is set incorrectly). Check your StatsAdapter class for errors. Post the code for that if you want us to take a look.Ossian
thanks for your help! added my StatsAdapter code to the postFibrillation
in your constructor for your adapter public StatsAdapter(String[] myDataset) add the line this.notifiyDataSetChanged(); - let me know if that helps at all.Ossian
@Fibrillation Did you try my answer, exactly?Cinda
What is the status of this?Cinda
B
21

Move your setLayoutManager line before setAdapter

Belia answered 15/7, 2015 at 4:24 Comment(0)
C
11

Create the adapter and the "non-view" related objects in onCreate. Then use your adapter for the RecyclerView.

Also, clean up your code by initiating the objects in a clear order based on their use and the Activity lifecycle.

public class StatsFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private StatsAdapter mAdapter; // was RecyclerView.Adapter mAdapter;
    private String[] myDataset = new String[]{"hello", "world", "yolo"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 1.
        mAdapter = new StatsAdapter(myDataset);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_stats, parent, false);

        // 2.
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        // 3.
        mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        return v;
    }
}
Cinda answered 17/6, 2015 at 15:23 Comment(3)
does mRecyclerView.setAdapter(mAdapter); go after part 3? Tried that and still getting the same errorFibrillation
Did you see all of my changes and comments? Just copy and paste the code.Cinda
@Jared can you add more explanation to "Create "non-view" related objects in onCreate". why is it different then adding it in OnCreateView?Lyric
J
3

One way to fix this issue is to attach an empty adapter to the RecyclerView

void initializeRecyclerView() {

   recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
   recyclerView.setAdapter(new SampleAdapter(getCurrentActivity()));
   recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity()));
   recyclerView.setHasFixedSize(true);
}
Joellenjoelly answered 4/9, 2015 at 5:55 Comment(1)
What if this error is happening in FirebaseRecyclerAdapter?Taper

© 2022 - 2024 — McMap. All rights reserved.