2013-02-26 63 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。