我的进度对话框有问题。请帮助我解决此问题。
private void uploadFile() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading Files, Please wait...");
progressDialog.show();
if (audioFileUri != null){
StorageReference imageReference = storageRef.child("updates_photo").child(engText.getText().toString().trim() + "");
StorageReference audioRef = storageRef.child("updates_audio").child(engText.getText().toString().trim() + ""); // storage location to firebase.
StorageReference fxRef = storageRef.child("updates_effect").child(engText.getText().toString().trim() + ""); // storage location to firebase
// Upload attach audio file
audioRef.putFile(audioFileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Audio Uploaded!", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Upload for attach effects audio file
if(audioFxUri != null){
fxRef.putFile(audioFxUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Audio Effect Uploaded! ", Toast.LENGTH_LONG).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) { // When loading progress is paused
progressDialog.cancel();
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) { // If progress fails
Toast.makeText(getApplicationContext(), "Audio Effect Failed! ", Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { // During upload progress
double progress = (100.0 * taskSnapshot.getBytesTransferred())/ taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + " % done");
}
});
}
// Upload for Image
if(imageFileUri != null){
imageReference.putFile(imageFileUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Image File Uploaded ", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
//and displaying error message
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
该错误显示在我的progressDialog.dismiss()中的onSuccess方法中; 您的帮助和建议将对我的项目有很大帮助。我希望得到一个好的答案。非常感谢,天哪!
答案 0 :(得分:0)
尝试将其添加到IF语句中
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();}
答案 1 :(得分:0)
我认为您和这里有同样的问题-> View not attached to window manager crash不确定,但一定要加油。
答案 2 :(得分:0)
karen如果单击时调用函数“ uploadFile()”,我认为您应该在异步过程开始之前调用dialog.show()。
代码可能像这样
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading Files, Please wait...");
buttonSubmit.OnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressDialog.show();
uploadFile();
}
});
private void uploadFile() {
if (audioFileUri != null){
StorageReference imageReference = storageRef.child("updates_photo").child(engText.getText().toString().trim() + "");
StorageReference audioRef = storageRef.child("updates_audio").child(engText.getText().toString().trim() + ""); // storage location to firebase.
StorageReference fxRef = storageRef.child("updates_effect").child(engText.getText().toString().trim() + ""); // storage location to firebase
// Upload attach audio file
audioRef.putFile(audioFileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Audio Uploaded!", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Upload for attach effects audio file
if(audioFxUri != null){
fxRef.putFile(audioFxUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Audio Effect Uploaded! ", Toast.LENGTH_LONG).show();
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) { // When loading progress is paused
progressDialog.cancel();
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) { // If progress fails
Toast.makeText(getApplicationContext(), "Audio Effect Failed! ", Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { // During upload progress
double progress = (100.0 * taskSnapshot.getBytesTransferred())/ taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + " % done");
}
});
}
// Upload for Image
if(imageFileUri != null){
imageReference.putFile(imageFileUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Image File Uploaded ", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
//and displaying error message
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}