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: ERROR/WindowManager(5886): android.view.WindowLeaked: Activity com.blah.MainActivity 泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010

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

4

1 回答 1

1

如果要在 Activity 销毁之前释放对话框,可以覆盖 onDestroy() 事件

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

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

希望有帮助

于 2011-08-20T00:20:07.130 回答