E/RecyclerView: No adapter attached; skipping layout (Using FRAGMENTS)

旧城冷巷雨未停 提交于 2020-08-03 09:14:08

问题


I modified my code according to the answers I found to similar questions but none of them worked. I am using TabFragments and RecyclerViews. The console displays the error "E/RecyclerView: No adapter attached; skipping layout" four times.

(When modifying it also gave me the error "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference", maybe they are correlated).

ADAPTER

public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.ViewHolder>{

public ArrayList<Ingredient> dataset = new ArrayList<>();
public Context ctx;


public static class ViewHolder extends RecyclerView.ViewHolder {

    public View view;

    public ViewHolder(View itemView) {
        super(itemView);
        view = itemView;
    }
}

public IngredientAdapter(Context ctx){
    super();
    this.ctx = ctx;
}

@Override
public IngredientAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient_row, parent, false);

        ViewHolder vh = null;
        vh = new ViewHolder(v);    

        return vh;
}

public void setDataset(ArrayList<Ingredient> dataset) {
    this.dataset.clear();
    this.dataset = dataset;
    this.notifyItemRangeInserted(0, this.dataset.size() - 1);
}

@Override
public void onBindViewHolder(IngredientAdapter.ViewHolder holder, int position) {
    ImageView image = (ImageView) holder.view.findViewById(R.id.ingredient_image);
    image.setImageBitmap(dataset.get(position).getImage());
    TextView name = (TextView)holder.view.findViewById(R.id.ingredient_name);
    name.setText(dataset.get(position).getName());
}

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

}

TABFRAGMENT

public class TabFragmentIngredients extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.recyclerview_list, container, false);
    rootView.setTag("RecyclerViewFragment");
    RecyclerView recycler = (RecyclerView) rootView.findViewById(R.id.recycler_view);


    final LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recycler.setLayoutManager(layoutManager);

    ArrayList ingredients = new ArrayList<Ingredient>();
    ingredients.add(new Ingredient("carrot", BitmapFactory.decodeResource(this.getContext().getResources(),R.drawable.carrot)));
    System.out.println(ingredients.size());
    IngredientAdapter adapter = new IngredientAdapter(this.getContext());
    adapter.setDataset(ingredients);
    recycler.setAdapter(adapter);

    return inflater.inflate(R.layout.recyclerview_list, container, false);
}

}


回答1:


Change return inflater.inflate(R.layout.recyclerview_list, container, false); to return rootView;




回答2:


I put in the adapter:

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

and in the Tab Fragment:

return rootView;


来源:https://stackoverflow.com/questions/38577023/e-recyclerview-no-adapter-attached-skipping-layout-using-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!