0

我在 Activity example.activity.SetupManage 中。经过一些操作后,我调用了一个显示通知消息的对话框。但即使我不点击 OK 按钮,所有后续代码都会执行,所以当我杀死所有 Activity 时,对话框会产生问题。我能做些什么来解决我的问题?

    AlertDialog myAlertDialog;
    public void onDestroy(Bundle savedInstanceState){

    if(myAlertDialog.isShowing())
        myAlertDialog.dismiss();
}
  // *** some operations *** //
  try{
        myAlertDialog = new AlertDialog.Builder(this)
        .setTitle("Notification")
        .setMessage("Operation Failed")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

        }
    })
    .show();

    }
    catch(Exception ex){

    }

    try{
    Intent myIntent = new Intent();
    myIntent.setClassName("example.activity", "example.activity.MainActivity");
    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
            Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
    }catch(Exception ex){

    }

错误信息:

   10-16 15:33:11.120: E/WindowManager(4360): Activity example.activity.SetupManage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@415ca638 that was originally added here
   10-16 15:33:11.120: E/WindowManager(4360):   at example.activity.SetupManage.CleanDatabase(SetupManage.java:61)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.view.ViewRoot.<init>(ViewRoot.java:288)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:249)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.view.Window$LocalWindowManager.addView(Window.java:532)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.app.Dialog.show(Dialog.java:269)
   10-16 15:33:11.120: E/WindowManager(4360):   at android.app.AlertDialog$Builder.show(AlertDialog.java:907)
   10-16 15:33:11.120: E/WindowManager(4360):   at example.activity.SetupManage.CleanDatabase(SetupManage.java:61)
4

3 回答 3

2

它继续执行,因为这就是你告诉它要做的事情。如果您希望在关闭对话框时执行代码,请将其放在onClick对话框按钮中,或者onDismiss如果可用。

更重要的是,为什么首先会出现这些onDestroy()?放置对话框(或 99.8% 的时间调用另一个活动的意图)似乎是一个非常糟糕的地方。您将永远无法看到它,该活动现在正在被破坏。我建议重新考虑您的程序流程。

于 2012-10-16T14:12:54.547 回答
1
Replace this code , it can help you...pass getApplication context 

 try{
        myAlertDialog = new AlertDialog.Builder(getApplicationContext() )
        .setTitle("Notification")
        .setMessage("Operation Failed")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

        }
    })
    .show();

    }
    catch(Exception ex){

    }
于 2012-11-19T11:04:31.213 回答
0

将 AlertDialog 保存到变量中:

myAlertDialog=new AlertDialog.Builder(这个).....

在 onPause() 方法中,如果对话框处于活动状态,您应该关闭它!

于 2012-10-16T13:51:53.223 回答