"android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"

"android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"

我想在发送邮件时显示 AlertDialog。但我有这个例外。 WindowManager$BadTokenException这是什么意思?

pDialog = ProgressDialog.show(MainActivity.sContext , "", "Sending Mail...", true);

    RetreiveFeedTask task = new RetreiveFeedTask();

据我了解你的问题,问题是你正试图在不同的线程上更新 UI,这是不可能的。 RetreiveFeedTask 可能在不同的线程上执行。

您必须 运行 您的代码才能在 UI 线程上显示警告对话框。您可以通过将以下代码粘贴到您自己的代码中并将代码添加到方法 run().

中来实现
activity.runOnUiThread(new Runnable() {
  public void run() {

  }
}

这是因为您的 Activity 已关闭,您正在尝试使用您的 ProgressDialog

进行一些工作

可以用onPause方法处理

例如

protected void onPause() {

        super.onPause();
         if ((pDialog != null) && pDialog.isShowing())
             pDialog.dismiss();
         pDialog = null;
    }

BadTokenException:您不能通过不是 ActivityContext 显示 Dialog

使用:

ProgressDialog.show(MainActivity.this, "", "Sending Mail...", true);

参见:BadTokenException