尝试显示对话框时泄漏窗口

发布于 2024-12-31 22:04:27 字数 1108 浏览 2 评论 0原文

我有这样的 onCreate 方法代码

    AlertDialog.Builder builder = new AlertDialog.Builder(
                    Ex.this);
            builder.setMessage(
                    getResources().getString(R.string.title))
                    .setPositiveButton(
                            getResources().getString(R.string.ok),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                }
                            })
                    .setNegativeButton(
                            getResources().getString(R.string.Cancel),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                }
                            }).create().show();

,但我总是收到 leaked window 错误。谁能告诉我出了什么问题吗?我尝试用 getApplicationContext() 替换 Ex.this 但这没有帮助。

I have inside onCreate method code like this

    AlertDialog.Builder builder = new AlertDialog.Builder(
                    Ex.this);
            builder.setMessage(
                    getResources().getString(R.string.title))
                    .setPositiveButton(
                            getResources().getString(R.string.ok),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                }
                            })
                    .setNegativeButton(
                            getResources().getString(R.string.Cancel),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                }
                            }).create().show();

but I get leaked window error always. Can anybody tell me what is wrong ? I tried to replace Ex.this with getApplicationContext() but that didn't help.

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

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

发布评论

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

评论(1

写下不归期 2025-01-07 22:04:27

不要直接调用 dialog.show()。相反,请执行 onCreateDialog()

请参阅 API 演示以获取示例: http ://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html

Don't call dialog.show() directly. Instead, go through onCreateDialog().

See API Demos for examples: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html

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