为什么"窗口泄露"是由警报对话引起的?

时间:2015-12-02 17:17:08

标签: android memory-leaks dialog

我的活动中有一个AlertDialog,在活动结束时我想开始另一项活动。 问题是我无法点击对话框按钮(进行其他操作),因为对话框立即消失。 如果我删除了意图,一切正常。我已将dismiss()放在onStop()onPause()onDestroy()取消对话框,但似乎没有影响。 此外,我无法在Dialog中启动意图,因为我必须在开始新活动之前必须执行其他操作。 我不明白问题出在哪里...... 这是创建对话框的功能:

  public AlertDialog createDialog(){
    AlertDialog alert = null;
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckAvailability.this);
    alertDialog.setTitle("This is a repeating event.");
    alertDialog.setIcon(R.drawable.warning2);
    alertDialog.setPositiveButton("Save for this event only", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            event = false;
        }
    });
    alertDialog.setNegativeButton("Save for future events", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            event = true;
        }
    });
    alert = alertDialog.create();
    return alert;

我创建并显示对话框:

AlertDialog alertDialog2; alertDialog2 = createDialog(); alertDialog2.show();

在活动结束时,我想开始另一项活动:

    //alertDialog2.dismiss(); <-- with or without this line the effect is the same
Intent intent = new Intent(CheckAvailability.this, Homepage.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

这些是活动方法:

    @Override
public void onDestroy(){
    super.onDestroy();
    Log.d("METODO", "onDestroy");
    if ( alertDialog2!=null ){
        alertDialog2.dismiss();
    }
}
    @Override
       public void onPause(){
    super.onPause();
        Log.d("METODO", "onPause");
        if ( alertDialog2!=null){
        alertDialog2.dismiss();
    }
}
@Override
public void onStop(){
    super.onStop();
    Log.d("METODO", "onStop");
    if ( alertDialog2!=null ){
        alertDialog2.dismiss();
    }
}

我还没有记录,只是对话框立即消失,应用程序继续显示下一个活动。但是,如果我删除活动方法(onStop,onDestroy,onPause),我有这个日志:

12-02 12:10:27.317    2938-2938/com.example.simone.consulenzeapp E/WindowManager﹕ Activity com.example.simone.consulenzeapp.ACTIVITY.CheckAvailability has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{531162cc V.E..... R.....I. 0,0-684,262} that was originally added here
android.view.WindowLeaked: Activity com.example.simone.consulenzeapp.ACTIVITY.CheckAvailability has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{531162cc V.E..... R.....I. 0,0-684,262} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:281)
        at com.example.simone.consulenzeapp.ACTIVITY.CheckAvailability$ClickHandlerConfirm.onClick(CheckAvailability.java:131)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

    at com.example.simone.consulenzeapp.ACTIVITY.CheckAvailability$ClickHandlerConfirm.onClick(CheckAvailability.java:131) -----> is line: alertDialog2.show();

0 个答案:

没有答案