recylerView空对象引用

问题描述 投票:-1回答:1

这里是rcyclerView的适配器类。2020-05-26 17:07:36.128 16709-16709 / com.example.proje E / AndroidRuntime:致命异常:主要流程:com.example.proje,PID:16709java.lang.NullPointerException:尝试在com.example.proje.OzelAdapterRV.getItemCount(OzelAdapterRV.java:41)]上的空对象引用上调用虚拟方法'int java.util.ArrayList.size()'

package com.example.proje;

public class OzelAdapterRV extends RecyclerView.Adapter<OzelAdapterRV.MyViewHolder> {
    private ArrayList<Urun> urunler;
    private Context context;

    public OzelAdapterRV(Context context, ArrayList<Urun> urunler) {
        this.urunler = urunler;
        this.context=context; }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.liswlayout,parent,false);

        MyViewHolder vh=new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Urun u=urunler.get(position);
        holder.txtkod.setText(u.getUrunKodu());
        holder.txtadi.setText(u.getUrunAdi());
        holder.txtfiyat.setText(String.valueOf(u.getFiyat()));
        Picasso.get().load(u.getResim1()).into(holder.img_resim);
    }
    @Override
    public int getItemCount() {
        return urunler.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView txtadi;
        TextView txtkod;
        TextView txtfiyat;
        ImageView img_resim;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

             txtadi=itemView.findViewById(R.id.satirurunadi);
             txtkod=itemView.findViewById(R.id.satir_urunkodu);
             txtfiyat=itemView.findViewById(R.id.satirFiyat);
             img_resim=itemView.findViewById(R.id.imageView);
             img_resim.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Toast.makeText(context, "Resme Tıklandı", Toast.LENGTH_SHORT).show();
                     Urun secilenUrun=urunler.get(getAdapterPosition());
                     Intent i= new Intent(context,UrunEkleActivity.class);
                     i.putExtra("urunNesnesi",secilenUrun);
                     context.startActivity(i);
                 }
             });
        }
    }
}

这里是mainActivity,我在其中添加RecyclerView:

package com.example.proje;


public class Main2Activity extends AppCompatActivity {
    FirebaseAuth mAuth;
    private FirebaseUser mUser;

    TextView textView;
    FirebaseDatabase db;
    DatabaseReference ref;
    RecyclerView recyclerView;
    ArrayList<Urun> urunArrayList;
    OzelAdapterRV ozelAdapterRV;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tanimla();
       db=FirebaseDatabase.getInstance();
       ref=db.getReference("urunler");

        recyclerView=findViewById(R.id.rView);
        ozelAdapterRV=new OzelAdapterRV(this,urunArrayList);
        recyclerView.setAdapter(ozelAdapterRV);
        StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        ValueEventListener urunlerListener = ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                urunArrayList.clear();
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    Urun u = data.getValue(Urun.class);
                    urunArrayList.add(u);
                }
                ozelAdapterRV.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

         urunArrayList=new ArrayList<>();



    }

    public void tanimla(){
        mAuth = FirebaseAuth.getInstance();
        mUser=mAuth.getCurrentUser();
        textView=findViewById(R.id.txthosgeldin);
        textView.setText("Hoşgeldin "+mUser.getEmail());
    }


    }
}

这里xml代码是rcyclerView行:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_margin="10dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/txtbegensayisi"
            android:layout_width="44dp"
            android:layout_height="19dp"
            android:text="0"
            android:textAlignment="center"
            app:layout_constraintBottom_toBottomOf="@+id/imgbegen"
            app:layout_constraintEnd_toEndOf="@+id/imgbegen"
            app:layout_constraintStart_toStartOf="@+id/imgbegen"
            app:layout_constraintTop_toTopOf="@+id/imgbegen" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_launcher_background" />

        <ImageView
            android:id="@+id/imgbegen"
            android:layout_width="51dp"
            android:layout_height="49dp"
            android:layout_marginEnd="16dp"
            android:textAlignment="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView"
            app:srcCompat="@drawable/kalp" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/imgbegen"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView"
            app:layout_constraintVertical_bias="0.0">

            <TextView
                android:id="@+id/satir_urunkodu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="kod" />

            <TextView
                android:id="@+id/satirurunadi"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="ad" />

            <TextView
                android:id="@+id/satirFiyat"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="fiyat" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

这里是rcyclerView的适配器类。 2020-05-26 17:07:36.128 16709-16709 / com.example.proje E / AndroidRuntime:致命例外:主进程:com.example.proje,PID:16709 java.lang ....

java android android-studio android-recyclerview nullpointerexception
1个回答
0
投票
您将需要在开始时初始化列表:
© www.soinside.com 2019 - 2024. All rights reserved.