2014-02-22 164 views
1

在我的應用程序中,我嘗試覆蓋onKeyDown方法並調用自定義對話框,但運行到設備上時未顯示該自定義對話框。我用調試器,看到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 [email protected] that was originally added here 
02-22 17:40:30.653: E/WindowManager(18524): android.view.WindowLeaked: Activity com.example.FirstScreenActivity has leaked window [email protected] 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

Dialog顯示時,您正在完成Activity

這行導致完成您的活動

return super.onKeyDown(keyCode, event); 

當時您顯示Dialog導致泄露你Dialog窗口。

改變這一行

return super.onKeyDown(keyCode, event); 

return false; 

otherise overeride onBackpressed(),寫這個邏輯..

+0

oh..oh ...謝謝:) – Android