FATAL EXCEPTION:尝试在空对象引用getItemCount上调用接口方法'int java.util.List.size()'

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

我只是试图显示所有使用recyclerview上传到firebasestorage的图像。每次我运行我的应用程序以测试我的应用程序崩溃时,当我检查logcat时,它总是告诉我Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.myapp.yoody.AdapterOne.getItemCount,而我只是想知道它所引用的null对象是什么。它在谈论它没有连接到Firebase的事实吗?还是什么?我已经尝试了好几天使这个东西正常工作,但是我什么也没得到。请帮我在这里。我希望我的问题不会引起混淆。这是我的下面的代码。在此先感谢

     //Profilepage (The page where the images are supposed to display)    

           FirebaseAuth firebaseAuth;
        FirebaseUser firebaseUser;
        TextView name;
        DatabaseReference databaseReference;
        StorageReference storageReference;
        private Context mccontext=ProfileActivity.this;
        RecyclerView recyclerView;
        AdapterOne adapter1;
        String uid;
        // Folder path for Firebase Storage.

        ImageView imageView;

        //List<ImageUploadInfo> list=new ArrayList<>();
        ArrayList<ImageUploadInfo> imagesList;
        // Folder path for Firebase Storage.
        String Storage_Path = "Images/";

        // Root Database Name for Firebase Database.
        public static final String Database_Path = "Images";



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_profile);
            recyclerView=findViewById(R.id.recyclerView);
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
            recyclerView.setHasFixedSize(true);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
            overridePendingTransition(R.anim.slide_right, R.anim.slide_left);




            adapter1=new AdapterOne(mccontext,imagesList);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter1);
            imagesList=new ArrayList<>();
            adapter1.notifyDataSetChanged();
            //databaseReference=FirebaseDatabase.getInstance().getReference("Users");
            uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            // Assign FirebaseStorage instance to storageReference.
            storageReference = FirebaseStorage.getInstance().getReference();
            // Assign FirebaseDatabase instance with root database name.
            databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);

            // Adding Add Value Event Listener to databaseReference.

            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    for(DataSnapshot postSnapshot:dataSnapshot.getChildren() ){

                        ImageUploadInfo imageUploadInfo=postSnapshot.getValue(ImageUploadInfo.class);
                        imagesList.add(imageUploadInfo);

                    }
                    adapter1 = new AdapterOne(getApplicationContext(), imagesList);
                    recyclerView.setAdapter(adapter1);


                }



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

                }
            });





        }








        }

       //My Adapter class

       public class AdapterOne extends RecyclerView.Adapter<AdapterOne.ViewHolder> {
        Context context;
        List<ImageUploadInfo> imagesList;

        public AdapterOne(Context c, List<ImageUploadInfo> TempList) {
            imagesList = TempList;
            context = c;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            ImageUploadInfo imageUploadInfo = imagesList.get(position);


            Glide.with(context).load(imageUploadInfo.getImageUrl()).into(holder.imageView);
        }

        @Override
        public int getItemCount() {
             return imagesList.size();


        }



        public class ViewHolder extends RecyclerView.ViewHolder {

            public ImageView imageView;

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

                imageView=(ImageView) itemView.findViewById(R.id.imageview);
            }
        }
    }

     //UploadInfo class

     public class ImageUploadInfo {

   // public String imageName;

    public String imageURL;



    public ImageUploadInfo() {} // default constructor that takes no arguments

    public ImageUploadInfo( String url) {

        this.imageURL= url;
    }

    public String getImageUrl() {
        return imageURL;
    }


    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }
}

   //CardView class

   <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="vertical"
        >

        <GridLayout
            android:layout_width="match_parent"

            android:layout_height="fill_parent"
            android:layout_marginTop="0dp"
            android:alignmentMode="alignMargins"
            android:columnCount="1"
            android:paddingTop="20dp"
            android:columnOrderPreserved="false"
            android:rowCount="3">


            <!-- 3 -->


            <!-- 4 -->

            <androidx.cardview.widget.CardView
                android:layout_width="350dp"
                android:layout_height="320dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="4dp"
                android:layout_marginBottom="10dp"
                app:cardCornerRadius="0dp"
                app:cardElevation="0dp">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:id="@+id/imageview"
                     />

                <!--

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/imagename"
                    android:text="Image Name"
                    />
                -->
            </androidx.cardview.widget.CardView>



        </GridLayout>

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

Database Structure

android firebase android-recyclerview firebase-storage
1个回答
0
投票

问题是您将列表发送到适配器之后初始化了列表。因此,适配器尝试在不存在的列表上执行其方法。在将其发送到适配器之前移动初始化通道enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.