onKeyDown没有显示对话框

时间:2014-02-22 12:20:30

标签: android onkeydown customdialog

在我的应用程序中,我尝试覆盖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)

请帮忙。

感谢。

1 个答案:

答案 0 :(得分:1)

Activity正在显示时,您正在完成Dialog

此行导致完成您的活动

return super.onKeyDown(keyCode, event);

当时您正在显示Dialog,这会导致Dialog窗口泄漏。

更改此行

return super.onKeyDown(keyCode, event);

进入

return false;

otherise overeride onBackpressed()并写下这个逻辑..