I want to show a progress dialog while I'm processing some user's request. This is my code and I don't see any good reason why the dialog is not appearing on screen.
我想在处理一些用户的请求时显示进度对话框。这是我的代码,我没有看到为什么对话框没有出现在屏幕上的任何理由。
public void onGoClick(Button button) {
ProgressDialog progressDialog = null;
try {
if (invalid) {
// Show error message
} else {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setIndeterminate(true);
progressDialog.setMessage(getActivity().getResources().getString(R.string.user_wait_message));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
// Do processing
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
progressDialog.dismiss();
}
}
When I move the ProgressDialog declaration statement inside the try block and removes the dismiss method from finally block, I'm able to see the circular dialog on the screen, but when I change it like the above code, nothing comes on the screen. There is no async calls in the processing area, that's why I don't find anything buggy here. Any help is appreciated, thanks.
当我在try块中移动ProgressDialog声明语句并从finally块中删除dismiss方法时,我能够在屏幕上看到圆形对话框,但是当我像上面的代码一样更改它时,屏幕上没有任何内容。处理区域没有异步调用,这就是我在这里找不到任何错误的原因。任何帮助表示赞赏,谢谢。
2 个解决方案
#1
Your Progress dialog is shown and then immediately dismissed. The finally block is executed directly after the try block.
将显示“进度”对话框,然后立即将其解除。 finally块在try块之后直接执行。
Try to add an OnDismissListener to verify this for yourself.
尝试添加OnDismissListener以自行验证。
#2
It is very weird as clean and build worked for me. I don't know if it's a bug in Android Studio (1.2) or something else. I really appreciate your answers as it was very helpful and I've learned a lot of things about async in android :)
清洁和构建对我来说非常奇怪。我不知道这是Android Studio(1.2)中的错误还是别的。我非常感谢你的答案,因为它非常有用,我在android中学到了很多关于async的东西:)
#1
Your Progress dialog is shown and then immediately dismissed. The finally block is executed directly after the try block.
将显示“进度”对话框,然后立即将其解除。 finally块在try块之后直接执行。
Try to add an OnDismissListener to verify this for yourself.
尝试添加OnDismissListener以自行验证。
#2
It is very weird as clean and build worked for me. I don't know if it's a bug in Android Studio (1.2) or something else. I really appreciate your answers as it was very helpful and I've learned a lot of things about async in android :)
清洁和构建对我来说非常奇怪。我不知道这是Android Studio(1.2)中的错误还是别的。我非常感谢你的答案,因为它非常有用,我在android中学到了很多关于async的东西:)