Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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
尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;在我的适配器类中的空对象引用上_Java_Android_Android Recyclerview - Fatal编程技术网

尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;在我的适配器类中的空对象引用上

尝试调用虚拟方法';void android.widget.TextView.setText(java.lang.CharSequence)和#x27;在我的适配器类中的空对象引用上,java,android,android-recyclerview,Java,Android,Android Recyclerview,我的编码有这些错误 尝试在BindViewHolder(BrandAdapter.java:67)上的空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”,并 onBindViewHolder(BrandAdapter.java:34) 我不确定是什么地方出了问题,因为我非常仔细地学习了教程。我的数据库不是空的 它包含子对象 这是我的适配器类 public class BrandAdapter ext

我的编码有这些错误

尝试在BindViewHolder(BrandAdapter.java:67)上的空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”,并 onBindViewHolder(BrandAdapter.java:34)

我不确定是什么地方出了问题,因为我非常仔细地学习了教程。我的数据库不是空的

它包含子对象

这是我的适配器类

public class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.MyViewHolder> {

    private Context context;

    List<String> key;


    ArrayList<Brand> brandList;

    public BrandAdapter(ArrayList<Brand> brandList) {
        this.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();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView brand_name;
        CardView brand_cv;
        ImageView image, btnDelete;
        LinearLayout frame_layout;

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

            brand_name = itemView.findViewById(R.id.name);
            image = itemView.findViewById(R.id.image);
            brand_cv = itemView.findViewById(R.id.brand_cv);
            btnDelete = itemView.findViewById(R.id.delete);
            frame_layout = itemView.findViewById(R.id.frame_layout);

        }
    }

}

MyViewHolder
中,您试图查找布局中不存在的视图。因此,尝试将
brand\u name=itemView.findViewById(R.id.name)
更改为
brand\u name=itemView.findViewById(R.id.brand\u name)
。除了
图像
删除
之外,布局中的其他视图也存在同样的问题:您没有带有
品牌cv
框架布局
ID的视图。

非常感谢您的启发!谢谢,我甚至没有意识到我在适配器类中编写了不同的id并调用了不同的id。几个小时来一直在研究这个愚蠢的错误。非常感谢。
public class BrandActivity extends AppCompatActivity {



    DatabaseReference databaseReference;
    ArrayList<Brand> brandList;
    RecyclerView recyclerView;
    SearchView searchView;




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


        databaseReference = FirebaseDatabase.getInstance().getReference("Brand").child("");
        recyclerView = findViewById(R.id.rv);
        searchView = findViewById(R.id.searchView);

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (databaseReference != null) {
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        brandList = new ArrayList<>();

                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            brandList.add(ds.getValue(Brand.class));
                        }

                    }

                    BrandAdapter brandAdapter = new BrandAdapter(brandList);
                    recyclerView.setAdapter(brandAdapter);

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(BrandActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
}


<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    app:cardElevation="5dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/lightgray">

            <TextView
                android:id="@+id/brand_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="sample"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:textAlignment="center"
                android:background="@color/black"
                android:textSize="20sp"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline" />


            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="#FCFAFA"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/delete"
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:padding="8dp"
                    android:visibility="visible"
                    app:srcCompat="@drawable/trash" />


            </LinearLayout>
        </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000" />
</androidx.cardview.widget.CardView>

public class Brand {
    public String brand_id;
    public String brand_name;
    public String brand_image;

    public Brand(){}

    public Brand(String brand_id, String brand_name, String brand_image) {
        this.brand_id = brand_id;
        this.brand_name = brand_name;
        this.brand_image = brand_image;
    }

    public String getBrand_id() {
        return brand_id;
    }

    public void setBrand_id(String brand_id) {
        this.brand_id = brand_id;
    }

    public String getBrand_name() {
        return brand_name;
    }

    public void setBrand_name(String brand_name) {
        this.brand_name = brand_name;
    }

    public String getBrand_image() {
        return brand_image;
    }

    public void setBrand_image(String brand_image) {
        this.brand_image = brand_image;
    }
}