在AlertDialog中按OK后,活动泄露窗口

时间:2014-02-20 14:17:15

标签: android memory-leaks android-alertdialog

这是“泄漏窗口”和AlertDialog下的一个记录良好的问题。 我做了大量的研究,并作为一个基础的例子发布了“在AlertDialog在12月5日12点9点23分briana posy之后调用新意图”和其他人。 即使在Debug下,我也无法找到Window泄漏发生的位置和原因。 有人能给我一个解决方法吗? 提前谢谢。

     public void SendMessageDialog(String message) {

          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          if ( bkcode == 0) {       
               builder.setIcon(R.drawable.ecg);                     /* SET OK ICON */
               message = BKP_OK;
          }else {
               builder.setIcon(R.drawable.bad);               /* SET NOK ICON */
               builder.setTitle("My MESSAGE")
               .setMessage(message)                           
               .setCancelable(true)                             // allow the use of Back Button on the hardware 
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void  onClick(DialogInterface dialog, int id) {  
                    dialog.dismiss(); 
                    returnMain();

        }                 
       });
     final AlertDialog alert = builder.create(); 
     alert.show();      
      } 
     ///////////////////////////////////////////////////////////////////
     //             BACK TO THE CALLING ACTIVITY
     ///////////////////////////////////////////////////////////////////
     public void returnMain() {
          Intent intent = new Intent(CheckSDDB.this, MainActivity.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          CheckSDDB.this.startActivity(intent);

 }

日志:

 02-20 07:39:45.642: E/WindowManager(5412): Activity com.peter.databasetest.CheckSDDB has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41218330 that was originally added   here
 02-20 07:39:45.642: E/WindowManager(5412): android.view.WindowLeaked: Activity com.peter.databasetest.CheckSDDB has leaked window      com.android.internal.policy.impl.PhoneWindow$DecorView@41218330 that was originally added here
 02-20 07:39:45.642: E/WindowManager(5412):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.view.Window$LocalWindowManager.addView(Window.java:547)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.Dialog.show(Dialog.java:277)
 02-20 07:39:45.642: E/WindowManager(5412):     at com.peter.databasetest.CheckSDDB.SendMessageDialog(CheckSDDB.java:377)
 02-20 07:39:45.642: E/WindowManager(5412):     at com.peter.databasetest.CheckSDDB.checkDatabase(CheckSDDB.java:233)
 02-20 07:39:45.642: E/WindowManager(5412):     at com.peter.databasetest.CheckSDDB.onCreate(CheckSDDB.java:113)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.Activity.performCreate(Activity.java:5008)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.os.Handler.dispatchMessage(Handler.java:99)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.os.Looper.loop(Looper.java:137)
 02-20 07:39:45.642: E/WindowManager(5412):     at android.app.ActivityThread.main(ActivityThread.java:4745)
 02-20 07:39:45.642: E/WindowManager(5412):     at java.lang.reflect.Method.invokeNative(Native Method)
 02-20 07:39:45.642: E/WindowManager(5412):     at java.lang.reflect.Method.invoke(Method.java:511)
 02-20 07:39:45.642: E/WindowManager(5412):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 02-20 07:39:45.642: E/WindowManager(5412):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 02-20 07:39:45.642: E/WindowManager(5412):     at dalvik.system.NativeStart.main(Native Method)

@cYrixmorten:对不起延迟回复。你的代码运行正常。为你+1。

1 个答案:

答案 0 :(得分:0)

如果你这样做会有什么不同:

 public void returnMain() {
      if (alert != null && alert.isShowing()) {
          alert.dismiss();
      }
      Intent intent = new Intent(CheckSDDB.this, MainActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      CheckSDDB.this.startActivity(intent);
}