2013-02-26 60 views
0

您好我已經開發的Android應用程序的PhoneGap和時,將顯示對話框,屏幕方向的變化,呈現出logcat.How錯誤來解決這個在Android設備上查看windowleaked

這裏是我的logcat錯誤:

E/WindowManager(5759): Activity com.example.Service.NotificationAlert has leaked window [email protected] that was originally added here 
E/WindowManager(5759): android.view.WindowLeaked: Activity com.example.Service.NotificationAlert has leaked window [email protected] that was originally added here 
E/WindowManager(5759): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344) 
E/WindowManager(5759): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 
E/WindowManager(5759): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
E/WindowManager(5759): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
E/WindowManager(5759): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
E/WindowManager(5759): at android.app.Dialog.show(Dialog.java:278) 
E/WindowManager(5759): at com.example.Service.NotificationAlert$1.handleMessage(NotificationAlert.java:103) 
E/WindowManager(5759): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/WindowManager(5759): at android.os.Looper.loop(Looper.java:137) 
E/WindowManager(5759): at android.app.ActivityThread.main(ActivityThread.java:4424) 
E/WindowManager(5759): at java.lang.reflect.Method.invokeNative(Native Method) 
E/WindowManager(5759): at java.lang.reflect.Method.invoke(Method.java:511) 
E/WindowManager(5759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
E/WindowManager(5759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
E/WindowManager(5759): at dalvik.system.NativeStart.main(Native Method) 

請告訴我解決方案。謝謝提前。

這裏是我的代碼:

AlertDialog alertDialog = new AlertDialog.Builder(NotificationAlert.this).create(); 
    alertDialog.setTitle("Mobilyzer"); 
    msgCountBundle = getIntent().getExtras(); 
    messageCount = msgCountBundle.getInt("Count"); 
    userId = msgCountBundle.getInt("userid"); 
    if (messageCount > 1) { 
     alertDialog 
       .setMessage("You have " + messageCount + " New Messages"); 
    } else { 
     alertDialog.setMessage("You have " + messageCount + " New Message"); 
    } 

alertDialog.show(); 
new Timer().schedule(new task(), 30000); 

private class task extends TimerTask 
{ 
    public void run() 
    { 
     toastHandler.sendEmptyMessage(0); 
    } 
} 

private final Handler toastHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     if(alertDialog.isShowing()) 
     { 
      try 
      { 
       alertDialog.dismiss(); 
       finish(); 
       Log.i("alertdialog","hide alert dialog"); 
      } 
      catch(IllegalArgumentException e) 
      { 
       Log.e("illegal ","illegal exception in dialog"+e); 
      } 
      catch(Exception e) 
      { 
       Log.e("illegal ","exception in dialog"+e); 
      } 
     } 
     Log.i("alertdialog","show alert dialog"); 
    } 
}; 
+1

粘貼一些代碼以獲得更準確的答案 – 2013-02-26 12:09:50

回答

0

NotificationAlertActivity就表明沒有通過顯式調用它dismiss()Activity被暫停駁回了Dialog。請保留對Dialog對象的參考,並在onPause()上對其調用dismiss()

@Override 
protected void onPause() { 
    if (myDialog != null) { 
     myDialog.dismiss(); 
    } 
    super.onPause(); 
} 
+0

感謝您的答覆。但我需要關閉timertask中的對話框。如何解決此問題... – JavaH 2013-02-26 12:20:35

+0

在'TimerTask'的run()結尾處,添加以下代碼:!'新的處理程序(Looper.getMainLooper())後(新的Runnable(){ \t \t \t @覆蓋 \t \t \t公共無效的run(){ \t \t \t \t如果(myDialog = NULL ){ \t \t \t \t \t myDialog.dismiss(); \t \t \t \t} \t \t \t} \t \t});' – 2013-02-26 12:27:15

+0

但是'TimerTask'必須完成它的run()''之前暫停Activity'。 **或**在'onPause()'中添加解僱代碼。 – 2013-02-26 12:29:28

0

檢查的要添加對話框超過一次我think.So應用程序。在應用程序的流程,試圖通過對活動視圖中添加只有一個對話框來解決。

0

這是因爲一些其他異常,應用程序崩潰,並且因爲對話框顯示窗口泄漏異常被拋出。

反正當對話框不爲空並且活動完成時發生窗口泄漏異常。因此,對話框必須在onPause()中設置爲null,如上所述,或者在開始intent之前,活動正在移動到新的屏幕。在這裏,您可以取消catch中的dailog,以便可以處理窗口泄漏異常,但是代碼中還存在其他一些異常。提供完整的logcat。