问题
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