【问题标题】:Link in AlertDialog leaks window?AlertDialog 泄漏窗口中的链接?
【发布时间】:2011-08-19 23:58:11
【问题描述】:

我在警告对话框中创建了一个可链接的文本,并使 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 那 最初是在这里添加的

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

【问题讨论】:

    标签: android android-alertdialog linkify


    【解决方案1】:

    如果你想在 Activity 被销毁之前释放对话框,你可以重写 onDestroy() 事件

    @Override
    public void onDestroy(){
        //Your dialog disposal code here
    
        super.onDestroy(); //Make sure you include this at the end.
    }
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2014-05-19
      • 1970-01-01
      • 2013-12-23
      相关资源
      最近更新 更多