2 myq 26 myq_26 于 2013.05.27 14:22 提问

onBackPressed() 显示错误

当在 mainactivity 中点击 back 按钮时,我想添加一个exit warning。使用的下面的代码:

@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:

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

如何修改?

1个回答

hxn_217
hxn_217   2013.05.27 17:38
@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();

    }
Csdn user default icon
上传中...
上传图片
插入图片