AlertDialog 中的链接泄漏窗口?

发布于 2024-11-30 15:46:39 字数 1200 浏览 0 评论 0原文

我在警报对话框中创建了一个可链接文本,并使 TextView 可单击,如下所示:

final SpannableString noRecords = new SpannableString("Sorry, no records could be found, please try again, or contact us at 867-5309");
Linkify.addLinks(noRecords);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("No Records Found")
    .setMessage(noRecords)
    .setCancelable(true)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

AlertDialog alert = builder.create();
alert.show();

((TextView)alert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

这有效,但是当单击它时,它会在 logcat 中产生错误:

08-19 19:40:55.753: 错误/WindowManager(5886): 活动 com.blah.MainActivity 已泄漏窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010 那个 最初添加于此处 08-19 19:40:55.753: 错误/WindowManager(5886):android.view.WindowLeaked:活动 com.blah.MainActivity 已泄漏窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010 那个 最初添加在这里

我认为这是因为在单击链接之前警报没有被解除。 有什么办法解决这个问题吗?我不想抛出任何错误。

I've created a linkable text in an alert dialog, and make the TextView clickable, like this:

final SpannableString noRecords = new SpannableString("Sorry, no records could be found, please try again, or contact us at 867-5309");
Linkify.addLinks(noRecords);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("No Records Found")
    .setMessage(noRecords)
    .setCancelable(true)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

AlertDialog alert = builder.create();
alert.show();

((TextView)alert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

This works, however when it's clicked it produces an error in logcat:

08-19 19:40:55.753: ERROR/WindowManager(5886): Activity
com.blah.MainActivity has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010 that
was originally added here 08-19 19:40:55.753:
ERROR/WindowManager(5886): android.view.WindowLeaked: Activity
com.blah.MainActivity has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010 that
was originally added here

I think this is because the alert is not dismissed before the link is clicked.
Is there any way around this? I'd prefer not to be throwing any errors.

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

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

发布评论

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

评论(1

两人的回忆 2024-12-07 15:46:39

如果您想在活动被销毁之前处理对话框,您可以覆盖 onDestroy() 事件

@Override
public void onDestroy(){
    //Your dialog disposal code here

    super.onDestroy(); //Make sure you include this at the end.
}

希望有帮助

If you want to dispose the dialog before the activity is destroyed, you can override the onDestroy() event

@Override
public void onDestroy(){
    //Your dialog disposal code here

    super.onDestroy(); //Make sure you include this at the end.
}

Hope that helps

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