2013-04-16 40 views
0

我正在覆蓋後退按鈕。我想在離開我的應用程序時向用戶顯示確認對話框。在創建對話框我得到了以下錯誤無法創建提示對話框

Activity com.example.netlogger.ActivityLogin has leaked window [email protected] that was originally added here 
04-16 16:47:40.885: E/WindowManager(7757): android.view.WindowLeaked: Activity com.example.netlogger.ActivityLogin has leaked window [email protected] that was originally added here 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.<init>(ViewRoot.java:247) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.Window$LocalWindowManager.addView(Window.java:424) 
04-16 16:47:40.885: E/WindowManager(7757): at android.app.Dialog.show(Dialog.java:241) 
04-16 16:47:40.885: E/WindowManager(7757): at com.example.netlogger.ActivityLogin.onBackPressed(ActivityLogin.java:130) 
04-16 16:47:40.885: E/WindowManager(7757): at android.app.Activity.onKeyUp(Activity.java:1888) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.KeyEvent.dispatch(KeyEvent.java:1063) 
04-16 16:47:40.885: E/WindowManager(7757): at android.app.Activity.dispatchKeyEvent(Activity.java:2068) 
04-16 16:47:40.885: E/WindowManager(7757): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1703) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2473) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2443) 
04-16 16:47:40.885: E/WindowManager(7757): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735) 
04-16 16:47:40.885: E/WindowManager(7757): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-16 16:47:40.885: E/WindowManager(7757): at android.os.Looper.loop(Looper.java:123) 
04-16 16:47:40.885: E/WindowManager(7757): at android.app.ActivityThread.main(ActivityThread.java:4633) 
04-16 16:47:40.885: E/WindowManager(7757): at java.lang.reflect.Method.invokeNative(Native Method) 
04-16 16:47:40.885: E/WindowManager(7757): at java.lang.reflect.Method.invoke(Method.java:521) 
04-16 16:47:40.885: E/WindowManager(7757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
04-16 16:47:40.885: E/WindowManager(7757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-16 16:47:40.885: E/WindowManager(7757): at dalvik.system.NativeStart.main(Native Method) 

我的代碼如下

@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     if (!prefs.getBoolean("ask_for_exit", false)) { 
      exitAlertDialog = new Builder(ActivityLogin.this) 
        .setTitle("Are You Sure?") 
        .setMessage("Are you sure you want to exit") 
        .setPositiveButton("YES", 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            finish(); 
           } 
          }) 
        .setNegativeButton("NO", new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            exitAlertDialog.dismiss(); 
           } 
        }).create(); 
      exitAlertDialog.show(); 

     } 
    } 

這是什麼錯誤意味着什麼?如果有任何線索,請告訴我。謝謝!

回答

1

問題出在super.onBackPressed();刪除該行。

super.onBackPressed(); 

退出應用程序。您正試圖在其後添加警報對話框。那就是問題所在。

+0

謝謝。你做對了 –

+0

很高興聽到:) –

2

如果你打電話

super.onBackPressed(); 
onBackPressed()方法

然後默認返回按鈕的行爲會發生,你的活動將不清洗(或顯示)被破壞了你的AlertDialog。你應該註釋掉這一行。

此外,如果你的SharedPreference是顯示AlertDialog,那麼你應該明確調用finish()else塊。

if (!prefs.getBoolean("ask_for_exit", false)){ 
    //show alert 
}else{ 
    finish(); 
} 
0

從功能刪除super.onBackPressed();,當使用的是,活動automatiacally調用的onStop();