我的编码遇到这些错误尝试在空对象引用 onBindViewHolder(BrandAdapter.java:67) 和 BrandAdapter.onBindViewHolder(BrandAdapter.java:34) 上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”。当我非常仔细地遵循教程时,我不太确定出了什么问题。而且我的数据库不为空。它包含孩子。这是我的适配器类public class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.MyViewHolder> { private Context context; List<String> key; ArrayList<Brand> brandList; public BrandAdapter(ArrayList<Brand> brandList) { thi.brandList = brandList; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.brand_list, viewGroup, false); context = viewGroup.getContext(); return new BrandAdapter.MyViewHolder(view); } public BrandAdapter(Context c) { this.context = c; } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) { myViewHolder.brand_name.setText(brandList.get(i).getBrand_name()); Picasso.with(context).load(brandList.get(i).getBrand_image()).into(myViewHolder.image);// Picasso.with(mcontext).load(brand.getBrand_image()).into(myViewHolder.image); myViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String bName = brandList.get(i).getBrand_name(); String pic = brandList.get(i).getBrand_image(); Log.i("loz", bName); Intent intent = new Intent(context, updateBrand.class); intent.putExtra("brand_name", bName); intent.putExtra("imgurl", pic); context.startActivity(intent); } }); @Override public int getItemCount() { return brandList.size(); } }}
1 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
在您的MyViewHolder
布局中,您试图查找布局中不存在的视图。所以,尝试改变brand_name = itemView.findViewById(R.id.name)
为brand_name = itemView.findViewById(R.id.brand_name)
。同样的问题也与布局中的其他视图有关,除了image
和delete
:您没有带有brand_cv
和frame_layout
id 的视图。
添加回答
举报
0/150
提交
取消