android泄露窗口强制关闭

时间:2013-09-26 01:15:56

标签: android android-activity android-dialog

我在我的应用中集成了zxing条形码扫描仪。当扫描程序进行扫描时,它会获取结果并通过异步任务搜索外部在线数据库,然后根据结果打开新活动。我得到一个不一致的力量关闭,每次扫描都不会发生,有时候只是因此我无法弄清楚为什么我会出现泄漏的窗口错误,而且有时只是:

09-25 21:02:48.105  24371-24371/com.beerportfolio.beerportfoliopro E/WindowManager﹕ Activity com.example.beerportfoliopro.BeerPage2 has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@424b7198 that was originally added here
        android.view.WindowLeaked: Activity com.example.beerportfoliopro.BeerPage2 has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@424b7198 that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:468)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351)
        at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171)
        at android.view.Window$LocalWindowManager.addView(Window.java:558)
        at android.app.Dialog.show(Dialog.java:282)
        at com.example.beerportfoliopro.GetBeerRateJSON.onPreExecute(GetBeerRateJSON.java:52)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.example.beerportfoliopro.GetBeerDataJSON.onPostExecute(GetBeerDataJSON.java:111)
        at com.example.beerportfoliopro.GetBeerDataJSON.onPostExecute(GetBeerDataJSON.java:34)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5536)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
        at dalvik.system.NativeStart.main(Native Method)
09-25 21:02:48.265  24371-24371/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.IllegalArgumentException: View not attached to window manager
        at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:784)
        at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:476)
        at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:182)
        at android.app.Dialog.dismissDialog(Dialog.java:328)
        at android.app.Dialog.dismiss(Dialog.java:311)
        at com.example.beerportfoliopro.GetBeerRateJSON.onPostExecute(GetBeerRateJSON.java:74)
        at com.example.beerportfoliopro.GetBeerRateJSON.onPostExecute(GetBeerRateJSON.java:27)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5536)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

从堆栈跟踪看,当Dialog完成其工作时,AsyncTask看起来不可见。在这种情况下,对dismiss()的调用将使应用程序崩溃。

您可以尝试使用Dialog.isShowing()来避免崩溃:

protected void onPostExecute(...) {
    ...
    if (dialog.isShowing()) {
        dialog.dismiss(); // or .cancel()
    }
    ...
}