windowleaked在android视图中

时间:2013-02-26 12:07:24

标签: java android screen-orientation

您好我开发了android phonegap应用程序,当显示对话框并且屏幕方向发生变化时,显示logcat中的错误。如何解决此问题

这是我的logcat错误:

E/WindowManager(5759): Activity com.example.Service.NotificationAlert has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@412c93c0 that was originally added here
E/WindowManager(5759): android.view.WindowLeaked: Activity com.example.Service.NotificationAlert has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@412c93c0 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");
    }
};

3 个答案:

答案 0 :(得分:0)

NotificationAlert Activity中,当Dialog暂停时,您显示的dismiss()未明确调用Activity。保留对Dialog对象的引用,并在其上onPause()调用dismiss()

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

答案 1 :(得分:0)

检查应用程序的流程。在应用程序中,我想多次添加对话框。所以,尝试通过在活动视图中只添加一个对话框来解决。

答案 2 :(得分:0)

由于某些其他异常,应用程序崩溃,并且因为对话框显示窗口泄漏异常被抛出。

无论如何,当对话框不为null且活动结束时,会发生窗口泄漏异常。因此,必须在onPause()中使对话框为空,如上所述,或者活动在启动意图之前移动到新屏幕。在这里,您可以使catch中的dailog无效,以便可以处理窗口泄漏异常,但代码中还有一些其他异常。提供完整的logcat。