【问题标题】:App crashing whilst displaying a dialog显示对话框时应用程序崩溃
【发布时间】:2023-03-23 14:32:02
【问题描述】:

当我尝试调用此方法时出现错误:

        okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, final IOException e) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showErrorAlert(e.toString());
                }
            });
        }

//The Method:

public void showErrorAlert(String error) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ProjectListActivity.this);

    dialogBuilder.setMessage("Hmm, there seems to be an error downloading the project list. " + error);
    dialogBuilder.setCancelable(true);

    dialogBuilder.setPositiveButton(
            "Okay",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = dialogBuilder.create();
    alert.show();
}

所以当调用失败时,它会立即崩溃,这是控制台中的输出:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.chimesoftware.chime.chimetimemanager, PID: 5770
                  android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@a9b7fff is not valid; is your activity running?
                      at android.view.ViewRootImpl.setView(ViewRootImpl.java:925)
                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                      at android.app.Dialog.show(Dialog.java:330)
                      at com.chimesoftware.chime.chimetimemanager.ProjectListActivity.showErrorAlert(ProjectListActivity.java:145)
                      at com.chimesoftware.chime.chimetimemanager.ProjectListActivity$1$1.run(ProjectListActivity.java:77)
                      at android.os.Handler.handleCallback(Handler.java:790)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6753)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这可能是由于错误调用错误吗?或者可能是因为我试图显示的错误类型。

谢谢。

【问题讨论】:

标签: java android dialog okhttp


【解决方案1】:
if(!((Activity) context).isFinishing()){
    //show dialog here
}

【讨论】:

  • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
【解决方案2】:

正如异常消息所说,当前活动已完成,但您正试图显示一个包含已完成活动上下文的对话框。由于对话框没有窗口显示android运行时抛出此异常。

使用Activity isFinishing() 方法:

检查此活动是否正在结束,可能是因为您对其调用了 finish() 或其他人已请求结束。

if(!isFinishing())
showDialog();

【讨论】:

    【解决方案3】:

    它对我有用,如果你想使用它:

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(HelpAndSupport.this);
        alertDialog.setTitle("Confirm Sign out...");
        alertDialog.setMessage("Are you sure you want signout from Talentslist?");
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //you posive click code here
            }
        });
    
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多