Android:当我想第二次显示进度对话框时出现 BadTokenException

发布于 2024-11-06 03:30:12 字数 994 浏览 3 评论 0原文

我有一个无法解决的问题...

在我的 Activity 中,我实例化了一个这样的类:

MapView mapView = (MapView) findViewById(R.id.mapview);
myMap = new Map(mapView, this);

构造函数如下所示

public Map(MapView mapView, Context context) {
    this.context = context;
    this.mapView = mapView;
}

我想要做的是在此类的过程中显示一个进度对话框,所以,在 Map 中,我得到了

private void showPath() {
    progressDialog = ProgressDialog.show(context, "Veuillez patienter", "Calcul de l'itinéraire en cours...", true, false);

    Thread thread = new Thread(this);
    thread.start();
}

When thread is over, I执行

progressDialog.dismiss();

这有效!但只有一次...如果我单击后退按钮,然后重新打开我的活动,我会得到一个 BadTokenException

05-06 23:27:15.941: ERROR/AndroidRuntime(1247): android.view.WindowManager$ BadTokenException:无法添加窗口 - 令牌 android.os.BinderProxy@44ecc8e8 无效;你的活动正在运行吗?

我已经尝试了我找到的所有解决方案,但没有一个有效...甚至使用扩展 AsyncTask 的类。

感谢您的帮助

I have a problem that I can't solve...

In my Activity, I instantiate a class like this :

MapView mapView = (MapView) findViewById(R.id.mapview);
myMap = new Map(mapView, this);

The constructor looks like this

public Map(MapView mapView, Context context) {
    this.context = context;
    this.mapView = mapView;
}

And what I want to do is to show a progressDialog during a process of this class, so, in Map, I got

private void showPath() {
    progressDialog = ProgressDialog.show(context, "Veuillez patienter", "Calcul de l'itinéraire en cours...", true, false);

    Thread thread = new Thread(this);
    thread.start();
}

When thread is over, I do

progressDialog.dismiss();

This works ! But only one time... If I click on the back button, and re open my activity, I got a BadTokenException

05-06 23:27:15.941: ERROR/AndroidRuntime(1247): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@44ecc8e8 is not valid; is your activity running?

I've tryed all solutions I found, but no one works... Even use a class who extends AsyncTask.

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-11-13 03:30:12

正如错误消息告诉您的那样,这是因为您尝试在活动中显示对话框,但活动没有运行(已经完成?)。因此,在显示对话框之前,您可能需要确保活动尚未完成:

public class MyActivity extends Activity {

    public void showDialog(Dialog dialog) {
        if (!isFinishing()) {
            // If activity is not finished, then show this dialog.
            dialog.show();
        }
    }

}

As what the error message told you, it's because you try to show a dialog in a Activity but the Activity is not running(have already been finished?). So before you show a dialog, you may want to make sure that the Activity is not finished:

public class MyActivity extends Activity {

    public void showDialog(Dialog dialog) {
        if (!isFinishing()) {
            // If activity is not finished, then show this dialog.
            dialog.show();
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文