Search code examples
javaandroidandroid-alertdialogbuilder

When I use Alert.Dialog the application crashes


I want to show prograssbar with the help of Alert.Dialog while saving data to database. but after registration the program crashes.

A method that gives an error after running:

private void saveData() {

        if (isPickPhoto) {
            StorageReference mRef = FirebaseStorage.getInstance().getReference().child("ShopList Images")
                    .child(imgUri.getLastPathSegment());
            AlertDialog.Builder builder = new AlertDialog.Builder(AddShopListActivity.this);
            builder.setCancelable(false);
            builder.setView(R.layout.progress_layout);
            AlertDialog dialog = builder.create();
            dialog.show();

            mRef.putFile(imgUri).addOnSuccessListener(taskSnapshot -> {
                Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                while (!uriTask.isComplete()) ;
                Uri urlImage = uriTask.getResult();
                imageUrl = urlImage.toString();
                uploadData();
                dialog.dismiss();
            }).addOnFailureListener(e -> {
                dialog.dismiss();
            });
        } else {
            Toast.makeText(this, "Yükleme yapmak için galeriden bir resim seç", Toast.LENGTH_SHORT).show();
        }
    }

but when I use it to update the data it works fine.

method that works flawlessly:

private void updateData() {
        AlertDialog.Builder builder = new AlertDialog.Builder(AddShopListActivity.this);
        builder.setCancelable(false);
        builder.setView(R.layout.progress_layout);
        AlertDialog dialog = builder.create();
        dialog.show();

        if (isPhotoChanged) {

            StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("ShopList Images").child(imgUri.getLastPathSegment());

            storageReference.putFile(imgUri).addOnSuccessListener(taskSnapshot -> {
                Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                while (!uriTask.isComplete()) ;
                Uri urlImage = uriTask.getResult();
                imageUrl = urlImage.toString();
                uploadData();
                dialog.dismiss();
            }).addOnFailureListener(e -> {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            });
        } else {
            imageUrl = oldImageUrl;
            uploadData();
            dialog.dismiss();
        }
    }

what can i do for using AlertDialog in saveData() metod ?

i want to show progressbar with Dialog.Alert.builder. but it crashes after data saved.

LogCat screen:

enter image description here


Solution

  • This particular exception seems to be a bit tricky to interpret, but here's a good explanation of what can cause this error. tl;dr: For your case the error appear to be the dialog is outliving the Activity it is called in, because the .dismiss()-call isn't being issued properly sometimes.

    Now it seems to be a bit tricky to narrow down the root, when the cause of your Activity shutting down prematurely won't show up in the logs. (There are reports of observing that in the comments under a couple of the answers of the SO post above.)
    So to find out what could be shutting down your method before the dialog could be dismissed, comment out the dialog.show(), reproduce the steps that caused this error to occur and check the logs again.