问题描述
我根据对类似问题的答案修改了代码,但没有一个起作用.我正在使用TabFragments和RecyclerViews.控制台显示四次错误"E/RecyclerView:未连接适配器;跳过布局".
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.
(修改时,还给了我错误"java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)) '在一个空对象引用上",也许它们是相关的.
(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).
适配器
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
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);
}
}
推荐答案
将return inflater.inflate(R.layout.recyclerview_list, container, false);
更改为return rootView;
这篇关于E/RecyclerView:未连接适配器;跳过布局(使用片段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!