0

当单击 mainactivity 中的后退按钮时,我想添加退出警告。为此,我正在使用此代码

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub

    final Dialog dialoge=new Dialog(MainActivity.this,android.R.style.Theme_Translucent_NoTitleBar);
    dialoge.setContentView(R.layout.popup_layout);

    Button yes=(Button)dialoge.findViewById(R.id.yes);
    yes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            MainActivity.this.finish();

        }
    });
    Button no=(Button)dialoge.findViewById(R.id.no);
    no.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialoge.cancel();
        }
    });


    dialoge.show();
}

这是有效的,但退出时显示一些错误。

Logcat 是

  03-21 10:25:57.192: E/WindowManager(14488): Activity com.example.design.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@447fec28 that was originally added here
03-21 10:25:57.192: E/WindowManager(14488): android.view.WindowLeaked: Activity com.example.design.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@447fec28 that was originally added here
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.ViewRoot.<init>(ViewRoot.java:251)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-21 10:25:57.192: E/WindowManager(14488):     at android.app.Dialog.show(Dialog.java:241)
03-21 10:25:57.192: E/WindowManager(14488):     at com.example.design.MainActivity.onBackPressed(MainActivity.java:97)
03-21 10:25:57.192: E/WindowManager(14488):     at android.app.Activity.onKeyUp(Activity.java:1895)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.KeyEvent.dispatch(KeyEvent.java:1283)
03-21 10:25:57.192: E/WindowManager(14488):     at android.app.Activity.dispatchKeyEvent(Activity.java:2075)
03-21 10:25:57.192: E/WindowManager(14488):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1673)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2493)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2463)
03-21 10:25:57.192: E/WindowManager(14488):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
03-21 10:25:57.192: E/WindowManager(14488):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 10:25:57.192: E/WindowManager(14488):     at android.os.Looper.loop(Looper.java:143)
03-21 10:25:57.192: E/WindowManager(14488):     at android.app.ActivityThread.main(ActivityThread.java:4914)
03-21 10:25:57.192: E/WindowManager(14488):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:25:57.192: E/WindowManager(14488):     at java.lang.reflect.Method.invoke(Method.java:521)
03-21 10:25:57.192: E/WindowManager(14488):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-21 10:25:57.192: E/WindowManager(14488):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-21 10:25:57.192: E/WindowManager(14488):     at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

6

您不会关闭对话框,这就是警告所说的泄漏。

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // here you need to dismiss your dialog before finishing activity
        if (dialoge != null) {
            dialoge.cancel();
            dialoge = null;
        }

        MainActivity.this.finish();

    }
于 2013-03-21T05:17:14.463 回答
2

您可以试一试,只需调用 yes 按钮,如下所示:

Button yes=(Button)dialoge.findViewById(R.id.yes);
    yes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialoge.cancel();
            MainActivity.this.finish();

        }
    });

退出应用程序之前需要取消对话框。这就是为什么它leaked window error 在对话框打开时显示。

于 2013-03-21T05:16:41.243 回答
1

也关闭对话框,调用dailog.dismiss()dialog.cancel()完成活动后,我测试了你的代码,它应该如下所示:

@Override
public void onBackPressed() {
    final Dialog dialoge=new Dialog(SecondActivity.this,android.R.style.Theme_Translucent_NoTitleBar);
    dialoge.setContentView(R.layout.popup_layout);

    Button yes=(Button)dialoge.findViewById(R.id.yes);
    yes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            finish();
            dialoge.cancel();

        }
    });
    Button no=(Button)dialoge.findViewById(R.id.no);
    no.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            dialoge.dismiss();
        }
    });


    dialoge.show();

}
于 2013-03-21T05:21:24.947 回答
1
dialoge.dismiss();

在您的 onClick 方法中实现 yes 按钮

于 2013-03-21T05:28:31.903 回答