当单击 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)