0

嗨,我已经开发了 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");
    }
};
4

3 回答 3

0

NotificationAlert Activity您显示的是在暂停时Dialog通过显式调用dismiss()它没有被解除的。Activity保留对您的Dialog对象的引用并onPause()调用dismiss()它。

@Override
protected void onPause() {
    if (myDialog != null) {
        myDialog.dismiss();
    }
    super.onPause();
}
于 2013-02-26T12:12:54.583 回答
0

检查应用程序的流程。在应用程序中,我认为您不止一次添加对话框。因此,尝试通过在活动视图中仅添加一个对话框来解决。

于 2013-02-26T12:15:36.530 回答
0

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

无论如何,当对话框不为空并且活动完成时,就会发生窗口泄漏异常。因此,对话框必须在上面提到的 onPause() 中设置为 null,或者在启动 Intent 之前,Activity 正在移动到新屏幕。在这里您可以取消catch中的dailog,以便可以处理窗口泄漏异常,但是您的代码中还有一些其他异常。提供完整的 logcat。

于 2013-02-26T12:49:07.493 回答