有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

RecyclerView适配器上的java上下文

我试图将上下文从java类发送到RecyclerView的适配器,这样我就可以单击元素并重定向到意图。显然,上下文不正确。我已经评论了它的台词,应用程序也可以运行,但只要我试图证明它,它就会再次崩溃。 我假设这是一个上下文的东西,但如果有另一种方法为我的元素设置一个click方法,这将非常有用

它引发的异常为空指针异常:

java.lang.RuntimeException: Unable to start activity: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.Button.setOnClickListener(安卓.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);

        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    将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);
    }
    

    虽然看起来你的问题是