2013-03-21 103 views
0

我想在mainactivity上單擊後退按鈕時添加退出警告。爲此,我正在使用此代碼onBackPressed()顯示錯誤

@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 [email protected] 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 [email protected] 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) 
+0

爲什麼你要顯示一個對話框,如果應用程序會關閉呢? – 2013-03-21 05:09:15

+0

退出手段? – 2013-03-21 05:14:56

+0

我想確保用戶真的想退出,而不是意外按回退鍵。 – 2013-03-21 05:15:50

回答

5

你並沒有放棄對話那是什麼警告說它泄漏。

@Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     // here you need to dismiss your dialog before finishing activity 
     if (dialoge != null) { 
      dialoge.cancel(); 
      dialoge = null; 
     } 

     MainActivity.this.finish(); 

    } 
+1

確保你檢查'dialoge'爲空! – Tushar 2013-03-21 05:20:10

+0

感謝兄弟,現在完美! – 2013-03-21 05:24:13

2

你可以試一試只需要調用是這樣的按鈕:

Button yes=(Button)dialoge.findViewById(R.id.yes); 
    yes.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      dialoge.cancel(); 
      MainActivity.this.finish(); 

     } 
    }); 

對話框需要退出應用程序之前被取消。這就是爲什麼當對話框打開時顯示leaked window error

+0

感謝兄弟,你是對的。 – 2013-03-21 05:25:32

+0

那麼剛剛投票! – 2013-03-21 05:26:11

+0

:-)已經完成! – 2013-03-21 05:28:35

1

辭退對話框也叫dailog.dismiss()dialog.cancel()完成活動後,我測試你的代碼,它應該如下:

@Override 
public void onBackPressed() { 
    final Dialog dialoge=new Dialog(SecondActivity.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 

      finish(); 
      dialoge.cancel(); 

     } 
    }); 
    Button no=(Button)dialoge.findViewById(R.id.no); 
    no.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
      dialoge.dismiss(); 
     } 
    }); 


    dialoge.show(); 

} 
1
dialoge.dismiss(); 

在按鈕

的是你的onClick方法實現