试图使用recyclerview显示Firebase存储中的所有图像

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

[我正在尝试使用回收者视图显示存储在Firebase存储器中的图像,但是当我运行我的应用程序时,我收到一条错误消息,说

Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.myapp.mee.ImageAdapter.getItemCount

下面是我的代码。

ImageAdapter

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{

    private Context mContext;
    private List<Upload> mUploads;

    public ImageAdapter(Context context,List<Upload> uploads){
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View V = LayoutInflater.from(mContext).inflate(R.layout.cardview, parent, false);
        return new ImageViewHolder(V);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        Upload uploadCurrent=mUploads.get(position);
        //Glide.get(mContext).load(uploadCurrent.getmImageUrl()).fit().into(holder.imageView);
        Picasso.get().load(uploadCurrent.getmImageUrl()).fit().centerCrop().into(holder.imageView);
    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageView;

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

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

上传

 public class Upload {
    private String mImageUrl;

    public Upload(){
        //Empty constructor needed
    }

    public Upload (String imageUrl){
        mImageUrl=imageUrl;
    }

    public String getmImageUrl() {
        return mImageUrl;
    }

    public void setmImageUrl(String mImageUrl) {
        this.mImageUrl = mImageUrl;
    }
}

ProfileActivity(显示所有图像的位置)

public class ProfileActivity extends AppCompatActivity {

   private RecyclerView recyclerView;
   private ImageAdapter mAdapter;

   private DatabaseReference databaseReference;
   private List<Upload> mUploads;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        overridePendingTransition(R.anim.slide_right, R.anim.slide_left);

        mAdapter=new ImageAdapter(ProfileActivity.this, mUploads);
        mUploads=new ArrayList<>();

        recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        databaseReference=FirebaseDatabase.getInstance().getReference("Users/images");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot postsnapshot:dataSnapshot.getChildren()){
                    Upload upload=postsnapshot.getValue(Upload.class);
                    mUploads.add(upload);
                }
                mAdapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ProfileActivity.this,databaseError.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

Database Structure

java android firebase-realtime-database android-recyclerview firebase-storage
2个回答
0
投票

在您的ImageAdapter中,您有一个私人成员字段:


0
投票

尝试下面的代码,首先初始化列表,然后将其分配给构造函数。

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