在我的应用程序中,我尝试覆盖onKeyDown
方法并调用自定义对话框,但是当运行到设备上时,它没有显示该自定义对话框。我使用了调试器,看到调用了onKeyDown
方法,但对话框没有出现,应用程序自行退出。
这是我onKeyDown
方法,
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
LayoutInflater factory = LayoutInflater.from(FirstScreenActivity.this);
View DialogView = factory.inflate(R.layout.exit_dialog, null);
final Dialog exit_dialog = new Dialog(FirstScreenActivity.this,R.style.Theme_Dialog);
exit_dialog.setContentView(DialogView);
exit_dialog.show();
Button btnYes = (Button) exit_dialog.findViewById(R.id.btnYes);
btnYes.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
android.os.Process.killProcess(android.os.Process.myPid());
}
});
Button btnNo = (Button) exit_dialog.findViewById(R.id.btnNo);
btnNo.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
exit_dialog.dismiss();
}
});
}
return super.onKeyDown(keyCode, event);
}
并且在 LogCat 中也收到此错误,
02-22 17:40:30.653: E/WindowManager(18524): Activity com.example.FirstScreenActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@417e7348 that was originally added here
02-22 17:40:30.653: E/WindowManager(18524): android.view.WindowLeaked: Activity com.example.FirstScreenActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@417e7348 that was originally added here
02-22 17:40:30.653: E/WindowManager(18524): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:415)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.Window$LocalWindowManager.addView(Window.java:557)
02-22 17:40:30.653: E/WindowManager(18524): at android.app.Dialog.show(Dialog.java:277)
02-22 17:40:30.653: E/WindowManager(18524): at com.example.onKeyDown(FirstScreenActivity.java:173)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.KeyEvent.dispatch(KeyEvent.java:2715)
02-22 17:40:30.653: E/WindowManager(18524): at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
02-22 17:40:30.653: E/WindowManager(18524): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2071)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3960)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3908)
02-22 17:40:30.653: E/WindowManager(18524): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3042)
02-22 17:40:30.653: E/WindowManager(18524): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 17:40:30.653: E/WindowManager(18524): at android.os.Looper.loop(Looper.java:137)
02-22 17:40:30.653: E/WindowManager(18524): at android.app.ActivityThread.main(ActivityThread.java:4963)
02-22 17:40:30.653: E/WindowManager(18524): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 17:40:30.653: E/WindowManager(18524): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 17:40:30.653: E/WindowManager(18524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-22 17:40:30.653: E/WindowManager(18524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-22 17:40:30.653: E/WindowManager(18524): at dalvik.system.NativeStart.main(Native Method)
请帮忙。
谢谢。
当显示Dialog
时,您正在完成Activity
。
此行导致完成您的活动
return super.onKeyDown(keyCode, event);
那时您正在显示Dialog
导致Dialog
窗口泄漏。
更改此行
return super.onKeyDown(keyCode, event);
到
return false;
otherise overeride onBackpressed()
并编写此逻辑..