Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有按钮的Recyclerview适配器';行不通_Java_Android - Fatal编程技术网

Java 带有按钮的Recyclerview适配器';行不通

Java 带有按钮的Recyclerview适配器';行不通,java,android,Java,Android,我想用两个按钮创建recycleview。我已在适配器中声明了按钮,但出现错误: “java.lang.NullPointerException:尝试调用虚拟方法'void' android.widget.Button.setOnClickListener(android.view.view$OnClickListener)' 在空对象引用上“ 这是我的adapter.java: public class ProductListAdapter extends RecyclerView.Adapt

我想用两个按钮创建
recycleview
。我已在
适配器中声明了按钮,但出现错误:

“java.lang.NullPointerException:尝试调用虚拟方法'void' android.widget.Button.setOnClickListener(android.view.view$OnClickListener)' 在空对象引用上“

这是我的adapter.java:

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ProductViewHolder> {
private OnItemClickListener mListener;

public interface OnItemClickListener{
    void onAddClick(int position);
    void onEditClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}

private Context context;
private ArrayList product_name, product_id, product_kcl, product_fat, product_carbo, product_sugar, product_proteins, product_salt;

public static class ProductViewHolder extends RecyclerView.ViewHolder {
    TextView product_name_txt;
    public Button edit_button;
    public Button add_to_meal_button;

    public ProductViewHolder(View itemView, final OnItemClickListener listener) {
        super(itemView);
        product_name_txt = itemView.findViewById(R.id.item_name_txt);
        edit_button = itemView.findViewById(R.id.edit_button);
        add_to_meal_button = itemView.findViewById(R.id.add_meal_button);

        add_to_meal_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        listener.onAddClick(position);
                    }
                }

            }
        });
        edit_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        listener.onEditClick(position);
                    }
                }
            }
        });
    }
}
public ProductListAdapter(Context context, ArrayList product_id, ArrayList product_name, ArrayList product_kcl, ArrayList product_fat, ArrayList product_carbo, ArrayList product_sugar, ArrayList product_proteins, ArrayList product_salt) {
    this.context = context;
    this.product_name = product_name;
    this.product_id = product_id;
    this.product_kcl = product_kcl;
    this.product_fat = product_fat;
    this.product_carbo = product_carbo;
    this.product_sugar = product_sugar;
    this.product_proteins = product_proteins;
    this.product_salt = product_salt;
}
@Override
public ProductListAdapter.ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.product_item, parent, false);
    return new ProductViewHolder(view, mListener);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, final int position) {
    holder.product_name_txt.setText(String.valueOf(product_name.get(position)));
}

@Override
public int getItemCount() {
    return product_id.size();
}
公共类ProductListAdapter扩展了RecyclerView.Adapter{
私人监听者;
公共接口侦听器{
无效的单击(内部位置);
void onEditClick(int位置);
}
公共void setOnItemClickListener(OnItemClickListener侦听器){
mListener=监听器;
}
私人语境;
私有ArrayList产品名称、产品id、产品kcl、产品脂肪、产品碳水化合物、产品糖、产品蛋白质、产品盐;
公共静态类ProductViewHolder扩展了RecyclerView.ViewHolder{
TextView产品名称;
公共按钮编辑按钮;
公共按钮添加到菜单按钮;
public ProductViewHolder(查看项目视图,最终监听器){
超级(项目视图);
product\u name\u txt=itemviewbyd(R.id.item\u name\u txt);
编辑按钮=itemView.findViewById(R.id.edit按钮);
添加膳食按钮=itemView.findViewById(R.id.add膳食按钮);
将\添加到\按钮。setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
if(侦听器!=null){
int position=getAdapterPosition();
如果(位置!=RecyclerView.无位置){
onAddClick(位置);
}
}
}
});
编辑按钮.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(侦听器!=null){
int position=getAdapterPosition();
如果(位置!=RecyclerView.无位置){
listener.onEditClick(位置);
}
}
}
});
}
}
公共产品列表适配器(上下文上下文、ArrayList产品id、ArrayList产品名称、ArrayList产品kcl、ArrayList产品脂肪、ArrayList产品碳水化合物、ArrayList产品糖、ArrayList产品蛋白质、ArrayList产品盐){
this.context=上下文;
this.product\u name=产品名称;
this.product\u id=product\u id;
this.product_kcl=product_kcl;
this.product\u fat=product\u fat;
本品=本品;
这个。产品\糖=产品\糖;
这个。product_proteins=product_proteins;
这个。产品盐=产品盐;
}
@凌驾
public ProductListAdapter.ProductViewHolder onCreateViewHolder(视图组父级,int-viewType){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图=充气机。充气(R.layout.product\u项目,父项,false);
返回新的ProductViewHolder(视图,mListener);
}
@凌驾
BindViewHolder上的公共无效(ProductViewHolder,最终int位置){
holder.product_name_txt.setText(String.valueOf(product_name.get(position));
}
@凌驾
public int getItemCount(){
退货产品_id.size();
}
}

这是我的项目:


您可以在
添加膳食按钮=itemView.findViewById(R.id.add\u膳食按钮)上获得NPEas
添加膳食按钮
id不包括在布局中


要解决此问题,请将项目布局按钮的id从
add_To_mean_按钮
更改为
add_mean_按钮
Dodaj按钮的id是add_To_mean_按钮,而不是add_mean_按钮

我也对您的java代码进行了一些重构:

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ProductViewHolder> {
private static OnItemClickListener mListener;
private Context context;
private ArrayList product_name, product_id, product_kcl, product_fat, product_carbo, product_sugar, product_proteins, product_salt;



public ProductListAdapter(Context context, ArrayList product_id, ArrayList product_name, ArrayList product_kcl, ArrayList product_fat, ArrayList product_carbo, ArrayList product_sugar, ArrayList product_proteins, ArrayList product_salt) {
    this.context = context;
    this.product_name = product_name;
    this.product_id = product_id;
    this.product_kcl = product_kcl;
    this.product_fat = product_fat;
    this.product_carbo = product_carbo;
    this.product_sugar = product_sugar;
    this.product_proteins = product_proteins;
    this.product_salt = product_salt;
}
@Override
public ProductListAdapter.ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
     View view = inflater.inflate(R.layout.product_item, parent, false);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {
    holder.product_name_txt.setText(String.valueOf(product_name.get(position)));
    holder.add_to_meal_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null){
                int position = holder.getAdapterPosition();
                if (position != RecyclerView.NO_POSITION){
                    mListener.onAddClick(position);
                }
            }

        }
    });
    holder. edit_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null){
                int position = holder.getAdapterPosition();
                if (position != RecyclerView.NO_POSITION){
                    mListener.onEditClick(position);
                }
            }
        }
    });
    
}

@Override
public int getItemCount() {
    return product_id.size();
}
public interface OnItemClickListener{
    void onAddClick(int position);
    void onEditClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}

public static class ProductViewHolder extends RecyclerView.ViewHolder {
    TextView product_name_txt;
    public Button edit_button;
    public Button add_to_meal_button;

    public ProductViewHolder(View itemView) {
        super(itemView);
        product_name_txt = itemView.findViewById(R.id.item_name_txt);
        edit_button = itemView.findViewById(R.id.edit_button);
        add_to_meal_button = itemView.findViewById(R.id.add_to_meal_button);
    }
       
       
    
}
公共类ProductListAdapter扩展了RecyclerView.Adapter{
专用静态侦听器;
私人语境;
私有ArrayList产品名称、产品id、产品kcl、产品脂肪、产品碳水化合物、产品糖、产品蛋白质、产品盐;
公共产品列表适配器(上下文上下文、ArrayList产品id、ArrayList产品名称、ArrayList产品kcl、ArrayList产品脂肪、ArrayList产品碳水化合物、ArrayList产品糖、ArrayList产品蛋白质、ArrayList产品盐){
this.context=上下文;
this.product\u name=产品名称;
this.product\u id=product\u id;
this.product_kcl=product_kcl;
this.product\u fat=product\u fat;
本品=本品;
这个。产品\糖=产品\糖;
这个。product_proteins=product_proteins;
这个。产品盐=产品盐;
}
@凌驾
public ProductListAdapter.ProductViewHolder onCreateViewHolder(视图组父级,int-viewType){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图=充气机。充气(R.layout.product\u项目,父项,false);
返回新的ProductViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(最终产品视图持有人,最终整数位置){
holder.product_name_txt.setText(String.valueOf(product_name.get(position));
holder.add\u to\u按钮.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mListener!=null){
int position=holder.getAdapterPosition();
如果(位置!=RecyclerView.无位置){
mListener.onAddClick(位置);
}
}
}
});
holder.edit_button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mListener!=null){
int position=holder.getAdapterPosition();
如果(位置!=RecyclerView.无位置){
mListener.onEditClick(位置);
}
}
}
});
}
@凌驾
public int getItemCount(){
ret