简体   繁体   中英

App crashes when retrieving data from firebase database

I've created a storage reference which stores my images within a folder with a unique UserID (for the logged in user), so when they upload a picture with that storage reference, the image will go into that firestore location, as well as a database reference to that image. It all gets uploaded correctly and appears in my realtime database within a userID as well as the firebase storage. However, when trying to view it in a recycler view, the application crashes, if I just put both storage and database reference as an "uploads" folder at the root of the database and storage, it works fine, but when trying to do it so that the images are taken to a unique UserID folder within the uploads folder, it doesn't work.

The error code I get: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

After debugging, error begins after:

mDatabaseRef.addValueEventListener(new ValueEventListener() {

in the RecyclerView retrieve file code.

Here shows that when I press upload, the images and database references are taken to the correct location: 在此处输入图片说明

在此处输入图片说明

So the issue here is that it's unable to locate the images when retrieving them, so if someone could give me the correct getReference for this, or even if there's any other error, it will be most welcome.

Upload file task

    if (mImageUri != null) {
        userID = mFirebaseAuth.getCurrentUser().getUid();
        StorageReference fileReference = mStorageRef.child(userID + "/" + System.currentTimeMillis() + "." + getFileExtension(mImageUri));

        mUploadTask = fileReference.putFile(mImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mProgressBar.setProgress(0);
                            }
                        }, 500);

                        Toast.makeText(getActivity(), "Upload successful", Toast.LENGTH_SHORT).show();
                        Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
                        while (!urlTask.isSuccessful());
                        Uri downloadUrl = urlTask.getResult();

                        Upload upload = new Upload(mEditTextFileName.getText().toString().trim(), mEditTextFileTags.getText().toString(),downloadUrl.toString() );

                        String uploadId = userID + "/" + mDatabaseRef.push().getKey();
                        mDatabaseRef.child(uploadId).setValue(upload);

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                        mProgressBar.setProgress((int) progress);
                    }
                });
    }           

RecyclerView retrieve code

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads/");
    mDatabaseRef.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 = new ImageAdapter(getActivity(), mUploads);

            mRecyclerView.setAdapter(mAdapter);
        }

有一个空的 OnClickListener 用于我尚未在我的 ProfileFragment 页面(包含 recyclerview)上实现的按钮,我必须删除它才能使其正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM