【发布时间】:2020-11-01 03:41:56
【问题描述】:
我在我的应用程序中使用了一个 AlertDialog,当我需要进行一些网络操作时,该对话框会向用户显示一些文本并阻止用户与应用程序交互,直到网络操作完成。 当网络操作完成后,我会调用dismiss来关闭对话框,然后用户可以再次与应用程序交互。 我的代码是这样的:
//pseudo-code runs in UI/main thread
private void fun() {
//business code here...
mainExecutor.execute(new Runnable() {
@Override
public void run() {
int timeLeftSec = 10;
while (true)
{
final int timeLeft = timeLeftSec--;
final String msg = "Time left ";
mainHandler.post(new Runnable() {
@Override
public void run() {
ShowDialog(msg + " " + timeLeft + " s");
}
});
if (CheckIfNetworkOpDone() || timeLeft < 0) {
mainHandler.post(new Runnable() {
@Override
public void run() {
DismissProcessDialog();
}
});
break;
} else {
try {
Thread.sleep(1000);
}catch (Exception e)
{
//
}
}
}
}
}
}
private void ShowDialog(String info)
{
if (mainDialog == null || mainDialogTextView == null)
{
View dialogView = LayoutInflater.from(this).inflate(R.layout.processbar_dialog, null);
if (mainDialog == null)
{
mainDialog = new AlertDialog.Builder(this).create();
mainDialog.setView(dialogView);
mainDialog.setCancelable(false);
mainDialog.setCanceledOnTouchOutside(false);
}
if (mainDialogTextView == null)
{
mainDialogTextView = dialogView.findViewById(R.id.dialogTextView);
}
}
mainDialogTextView.setText(info);
mainDialog.show();
}
private void DismissProcessDialog()
{
if (mainDialog != null)
{
try {
Activity activity = (Activity) mainActivityContext;
if (activity == null || activity.isDestroyed() || activity.isFinishing())
{
return;
}
Context context = ((ContextWrapper)(mainDialog.getContext())).getBaseContext();
if (!(context instanceof Activity ))
{
return;
}
activity = (Activity) context;
if (activity == null || activity.isDestroyed() || activity.isFinishing())
{
return;
}
if (mainDialog != null && mainDialog.isShowing())
{
mainDialog.dismiss();
}
}
catch (Exception e)
{
//
}
}
}
@Override
protected void onDestroy() {
if (mainDialog != null && mainDialog.isShowing())
{
mainDialog.dismiss();
}
mainDialog = null;
super.onDestroy();
}
但谷歌播放后端显示有一个崩溃日志以关闭。例外是:
java.lang.IllegalArgumentException:
at android.view.WindowManagerGlobal.findViewLocked (WindowManagerGlobal.java:517)
at android.view.WindowManagerGlobal.removeView (WindowManagerGlobal.java:426)
at android.view.WindowManagerImpl.removeViewImmediate (WindowManagerImpl.java:126)
at android.app.Dialog.dismissDialog (Dialog.java:389)
at android.app.-$$Lambda$oslF4K8Uk6v-6nTRoaEpCmfAptE.run (Unknown Source:2)
at android.os.Handler.handleCallback (Handler.java:883)
at android.os.Handler.dispatchMessage (Handler.java:100)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7356)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:930)
ProgressDialog 已弃用,我改用 AlertDialog。我不使用 ProgressBar 的原因是,我想在网络操作完成之前阻止用户。我发现当 ProgressBar 显示时用户仍然可以按下按钮。
关于如何修复这种崩溃或如何处理这种情况以正确显示对话框,是否有任何最佳实践?
任何建议将不胜感激。谢谢!
【问题讨论】:
-
stackoverflow.com/a/44188192/3192693 看看这个。您需要检查活动状态和对话框状态以避免此类错误。
-
@Monster Brain 感谢您的建议,但您可能不会阅读我的代码,因为我已经检查了代码中的活动和对话框状态。但它仍然崩溃。
-
你试过dismissAllowingStateLoss()
-
错误发生在 DismissProcessDialog 还是 onDestroy 中?
-
我认为它应该出现在 DismissProcessDialog 中,但我无法从我没有显示任何代码的崩溃日志中获取它。我最近才添加 onDestroy,在此之前有一些崩溃日志。所以我想它应该来自 DismissProcessDialog。
标签: android android-alertdialog progressdialog android-progressbar crashlytics-android