RecyclerView 适配器的上下文

Context on Adapter for RecyclerView

我正在尝试将上下文从我的 java class 发送到我的 RecyclerView 适配器,这样我就可以单击元素并重定向到一个意图。显然,语境不对。我已经评论了它的行并且该应用程序可以运行,但是当我尝试证明它时,它又开始崩溃了。 我假设它是一个上下文的东西,但如果有另一种方法可以为我的元素设置点击方法,那将非常有帮助。

它抛出的异常是一个空指针异常:

java.lang.RuntimeException: Unable to start activity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

适配器代码如下:

public class SubsidioAdapter extends RecyclerView.Adapter<SubsidioAdapter.SubsidioViewHolder> {

    private Context context;
    private List<Producto> productoList;
    private LayoutInflater inflater;

    public SubsidioAdapter(Context context, List<Producto> productoList) {
        this.context = context;
        this.productoList = productoList;
        this.inflater = LayoutInflater.from(context);
    }


    @Override
    public SubsidioViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = this.inflater.inflate(R.layout.producto_item, null);
        return new SubsidioViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SubsidioViewHolder subsidioViewHolder, int position) {
        subsidioViewHolder.imageView.setImageResource(this.productoList.get(position).getFoto());
        subsidioViewHolder.textView1.setText(this.productoList.get(position).getNombre());
        subsidioViewHolder.textView2.setText(this.productoList.get(position).getDescripcion());
        subsidioViewHolder.textView3.setText(this.productoList.get(position).getPrecio());
        subsidioViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                context.startActivity(new
                        Intent(context, ModificarProductoActivity.class));
            }
        });
    }

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

    public class SubsidioViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView1;
        TextView textView2;
        TextView textView3;

        public SubsidioViewHolder(View itemView) {
            super(itemView);

            this.imageView = itemView.findViewById(R.id.fotoSub);
            this.textView1 = itemView.findViewById(R.id.descSub);
            this.textView2 = itemView.findViewById(R.id.nombreSub);
            this.textView3 = itemView.findViewById(R.id.precioSub);

        }
    }
}

为适配器项布局的根元素设置一个 id,然后通过 id 找到该视图并使用它而不是 itemView, 而不是:

subsidioViewHolder.itemView.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {
          ...
        }
    });

使用:

subsidioViewHolder.rootLayout.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
              ...
            }
        });

。请记住,您可以在 onCreateViewHolder() 内的 recyclerView 适配器中获取上下文,而无需像这样使用构造函数:

@Override
public SubsidioViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    context=viewGroup.getContext();
    View view = this.inflater.inflate(R.layout.producto_item, null);
    return new SubsidioViewHolder(view);
}

尽管您的问题似乎出在