Android 如何解决资源未找到异常&;onBindViewHolder?

Android 如何解决资源未找到异常&;onBindViewHolder?,android,Android,我在recyclerView中添加了加减按钮,但当我运行我的应用程序时,应用程序崩溃了。错误显示,资源未找到异常&另一个错误显示在BindViewHolder上 我已尝试在RecyclerView中解决productAdapter onBindViewHolder问题 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.and

我在recyclerView中添加了加减按钮,但当我运行我的应用程序时,应用程序崩溃了。错误显示,资源未找到异常&另一个错误显示在BindViewHolder上

我已尝试在RecyclerView中解决productAdapter onBindViewHolder问题

        <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <androidx.cardview.widget.CardView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="12">

                        <androidx.appcompat.widget.AppCompatCheckBox
                            android:id="@+id/checkid"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" />

                        <ImageView
                            android:id="@+id/imageView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="2"
                            android:padding="4dp" />

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="9"
                            android:orientation="vertical">

                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/textViewTitleid"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="5dp"
                                    android:layout_toRightOf="@id/imageView"
                                    android:text="Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)"
                                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                                    android:textColor="#000000" />

                                <TextView
                                    android:id="@+id/textViewShortDescid"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@id/textViewTitleid"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginTop="5dp"
                                    android:layout_toRightOf="@id/imageView"
                                    android:text="13.3 Inch, 256 GB"
                                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

                            </LinearLayout>

                            <!--<TextView-->
                            <!--android:id="@+id/textViewRatingid"-->
                            <!--android:layout_width="wrap_content"-->
                            <!--android:layout_height="wrap_content"-->
                            <!--android:layout_below="@id/textViewShortDescid"-->
                            <!--android:layout_marginLeft="5dp"-->
                            <!--android:layout_marginTop="5dp"-->
                            <!--android:layout_toRightOf="@id/imageView"-->
                            <!--android:background="@color/colorPrimary"-->
                            <!--android:paddingLeft="15dp"-->
                            <!--android:paddingRight="15dp"-->
                            <!--android:text="4.7"-->
                            <!--android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"-->
                            <!--android:textStyle="bold" />-->
                            <RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content">

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignParentLeft="true"
                                    android:orientation="horizontal">

                                    <TextView
                                        android:id="@+id/textViewPriceid"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="INR 56990"
                                        android:textStyle="bold" />
                                </LinearLayout>

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignParentRight="true"

                                    >

                                    <Button
                                        android:id="@+id/minusbtn"
                                        android:layout_width="35dp"
                                        android:layout_height="35dp"
                                        android:background="@null"
                                        android:text="-"
                                        android:textSize="16dp" />

                                    <TextView
                                        android:id="@+id/counter_text"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:background="#DFDDDD"
                                        android:padding="2dp"
                                        android:text="0" />

                                    <Button
                                        android:id="@+id/plusbtn"
                                        android:layout_width="35dp"
                                        android:layout_height="35dp"
                                        android:background="@null"
                                        android:text="+"
                                        android:textSize="16dp" />


                                </LinearLayout>

                            </RelativeLayout>
                        </LinearLayout>

                    </LinearLayout>

                </androidx.cardview.widget.CardView>


            </LinearLayout>



        <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </androidx.recyclerview.widget.RecyclerView>

        </androidx.constraintlayout.widget.ConstraintLayout>





                package com.example.mycart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private Context context;
    private List<Product> productList;
    //static int count =0;


    public ProductAdapter(Context context, List<Product> productList) {
        this.context = context;
        this.productList = productList;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list, null);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ProductViewHolder holder, int position) {
        final Product product = productList.get(position);
        holder.textViewTitle.setText(product.getTitle());
        holder.textViewDesc.setText(product.getShortdesc());
        // holder.textViewRating.setText(String.valueOf(product.getRating()));
        holder.textViewPrice.setText(String.valueOf(product.getPrice()));
        holder.imageView.setImageDrawable(context.getResources().getDrawable(product.getImage()));
        //holder.counterText.setText(String.valueOf(getItemCount()));

        holder.count.setText(String.valueOf(product.getCount()));
        holder.plusbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                product.setCount(product.getCount() + 1);
                holder.count.setText(String.valueOf(product.getCount()));
                //product.setCount(product.getCount() + 1);
               //holder.count.setText(String.valueOf(product.getCount()+1));
                // product.setCount(product.getCount() + 1);
                //holder.count.setText(product.getCount());
            }
        });

        holder.minusbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (product.getCount() > 1) {
                    product.setCount(product.getCount() - 1);
                    holder.count.setText(String.valueOf(product.getCount()));
                    //product.setCount(product.getCount() - 1);
                    //holder.count.setText(String.valueOf(product.getCount()-1));
                    //product.setCount(product.getCount() - 1);
                    //holder.count.setText(product.getCount());
                }
            }
        });
    }

    @Override
    public int getItemCount() {

        return productList.size();
    }


    public class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textViewTitle, textViewDesc, textViewPrice, count;
        AppCompatCheckBox checkBox;
        Button minusbtn, plusbtn;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            textViewTitle = itemView.findViewById(R.id.textViewTitleid);
            textViewDesc = itemView.findViewById(R.id.textViewShortDescid);
            // textViewRating =itemView.findViewById(R.id.textViewRatingid);
            textViewPrice = itemView.findViewById(R.id.textViewPriceid);
            count = itemView.findViewById(R.id.counter_text);
            checkBox = itemView.findViewById(R.id.checkid);
            minusbtn = itemView.findViewById(R.id.minusbtn);
            plusbtn = itemView.findViewById(R.id.plusbtn);

        }
    }

}



                package com.example.mycart;

                public class Product {
                    private int id;
                    private String title, shortdesc;
                    private double price;
                    private int image;
                    private int count = 1;


                    public Product(int id, String title, String shortdesc, double price, int image) {
                        this.id = id;
                        this.title = title;
                        this.shortdesc = shortdesc;
                        this.price = price;
                        this.image = image;
                    }

                    public void setCount(int count) {
                        this.count = count;
                    }

                    public int getCount() {
                        return count;
                    }

                    public int getId() {
                        return id;
                    }

                    public String getTitle() {
                        return title;
                    }

                    public String getShortdesc() {
                        return shortdesc;
                    }

                    public double getPrice() {
                        return price;
                    }

                    public int getImage() {
                        return image;
                    }

                }







        package com.example.mycart;

        import androidx.appcompat.app.AppCompatActivity;
        import androidx.recyclerview.widget.LinearLayoutManager;
        import androidx.recyclerview.widget.RecyclerView;

        import android.os.Bundle;

        import java.util.ArrayList;
        import java.util.List;

        public class MainActivity extends AppCompatActivity {
            RecyclerView recyclerView;
            ProductAdapter adapter;
            List<Product> productList;


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                productList = new ArrayList<>();
                recyclerView =(RecyclerView) findViewById(R.id.recyclerView);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

                productList.add(
                        new Product(
                                1,
                                "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra",
                                "13.3 inch, Silver, 1.35 kg",
                                6500.00,R.drawable.macbook));
                productList.add(
                        new Product(
                                1,
                                "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra",
                                "13.3 inch, Silver, 1.35 kg",
                                6500.00,R.drawable.macbook));
                productList.add(
                        new Product(
                                1,
                                "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra",
                                "13.3 inch, Silver, 1.35 kg",
                                6500.00,R.drawable.macbook));


                adapter = new ProductAdapter(this,productList);
                recyclerView.setAdapter(adapter);



            }
        }

包com.example.mycart;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.appcompat.widget.AppCompatCheckBox;
导入androidx.recyclerview.widget.recyclerview;
导入java.util.List;
公共类ProductAdapter扩展了RecyclerView.Adapter{
私人语境;
私有列表产品列表;
//静态整数计数=0;
公共ProductAdapter(上下文上下文,列表productList){
this.context=上下文;
this.productList=productList;
}
@非空
@凌驾
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图=充气机充气(R.layout.list,空);
返回新的ProductViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull final ProductViewHolder,int position){
最终产品=productList.get(位置);
holder.textViewTitle.setText(product.getTitle());
holder.textViewDesc.setText(product.getShortdesc());
//holder.textViewRating.setText(String.valueOf(product.getRating());
holder.textViewPrice.setText(String.valueOf(product.getPrice());
holder.imageView.setImageDrawable(context.getResources().getDrawable(product.getImage());
//holder.counterText.setText(String.valueOf(getItemCount());
holder.count.setText(String.valueOf(product.getCount());
holder.plusbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
product.setCount(product.getCount()+1);
holder.count.setText(String.valueOf(product.getCount());
//product.setCount(product.getCount()+1);
//holder.count.setText(String.valueOf(product.getCount()+1));
//product.setCount(product.getCount()+1);
//holder.count.setText(product.getCount());
}
});
holder.minusbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(product.getCount()>1){
product.setCount(product.getCount()-1);
holder.count.setText(String.valueOf(product.getCount());
//product.setCount(product.getCount()-1);
//holder.count.setText(String.valueOf(product.getCount()-1));
//product.setCount(product.getCount()-1);
//holder.count.setText(product.getCount());
}
}
});
}
@凌驾
public int getItemCount(){
返回productList.size();
}
公共类ProductViewHolder扩展了RecyclerView.ViewHolder{
图像视图图像视图;
TextView textViewTitle、textViewDesc、textViewPrice、count;
AppCompatCheckBox复选框;
按钮最小BTN,最大BTN;
public ProductViewHolder(@NonNull View itemView){
超级(项目视图);
imageView=itemView.findViewById(R.id.imageView);
textViewTitle=itemView.findViewById(R.id.textViewTitleid);
textViewDesc=itemView.findViewById(R.id.textViewShortDescid);
//textViewRating=itemView.findViewById(R.id.textViewRatingid);
textViewPrice=itemView.findViewById(R.id.textViewPriceid);
count=itemView.findviewbyd(R.id.counter\u text);
checkBox=itemView.findviewbyd(R.id.checkid);
minusbtn=itemView.findviewbyd(R.id.minusbtn);
plusbtn=itemView.findviewbyd(R.id.plusbtn);
}
}
}
包com.example.mycart;
公共类产品{
私有int-id;
私有字符串标题,shortdesc;
私人双价;
私有int图像;
私有整数计数=1;
公共产品(整数id、字符串标题、字符串shortdesc、双倍价格、整数图像){
this.id=id;
this.title=标题;
this.shortdesc=shortdesc;
这个价格=价格;
这个,我
holder.count.setText(String.valueOf(product.getCount() + 1));
holder.count.setText(String.valueOf(product.getCount() - 1));