在Android中使用对话框和webView泄露的窗口

时间:2014-09-22 11:23:26

标签: android android-webview android-dialog

在处理来自接收活动对象引用的Java类的多个webview时,我的代码中存在错误。

这是我的代码:

try {
            mActivity.runOnUiThread(new Runnable() {
                @SuppressLint("NewApi")
                public void run() {

                    if (webView == null)
                        return;

                    if (visibility) {
                        webView.setVisibility(View.VISIBLE);
                        webView.requestFocus();

                        if (dialog != null && !dialog.isShowing()) {
                            System.out.println("case1: " + dialog + " , activity: " + mActivity + ", mview " + webView);

                        } else {
                            dialog = WebViewUtils.showWebView(mActivity, webView);
                            System.out.println("case2 " + dialog + " , activity: " + mActivity + ", mview " + webView);
                        }
                        if (!mActivity.isFinishing()) {
                            dialog.show();
                        }
                    } else {
                        dialog.dismiss();
                        webView.setVisibility(View.GONE);
                    }
                }
            });
        } catch (Exception e) {
            System.out.println(System.out);
            Log.d(Constants.TAG, e.getMessage());
        }

这是错误堆栈:

09-22 14:12:21.682: E/WindowManager(1772): android.view.WindowLeaked: Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41fc30e8 V.E..... R.....ID 0,0-768,1184} that was originally added here
09-22 14:12:21.682: E/WindowManager(1772):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
09-22 14:12:21.682: E/WindowManager(1772):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
09-22 14:12:21.682: E/WindowManager(1772):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-22 14:12:21.682: E/WindowManager(1772):  at android.app.Dialog.show(Dialog.java:286)
09-22 14:12:21.682: E/WindowManager(1772):  at com.itnt.JavaTest$3.run(JavaTest.java:199)
09-22 14:12:21.682: E/WindowManager(1772):  at android.app.Activity.runOnUiThread(Activity.java:4713)
09-22 14:12:21.682: E/WindowManager(1772):  at com.itnt.JavaTest.setVisibility(JavaTest.java:180)
09-22 14:12:21.682: E/WindowManager(1772):  at com.itnt.JavaTest.showAd(JavaTest.java:122)
09-22 14:12:21.682: E/WindowManager(1772):  at com.itnt.JavaTest.showAd(JavaTest.java:139)
09-22 14:12:21.682: E/WindowManager(1772):  at com.example.test.MainActivity.onClick(MainActivity.java:53)
09-22 14:12:21.682: E/WindowManager(1772):  at android.view.View.performClick(View.java:4438)
09-22 14:12:21.682: E/WindowManager(1772):  at android.view.View$PerformClick.run(View.java:18422)
09-22 14:12:21.682: E/WindowManager(1772):  at android.os.Handler.handleCallback(Handler.java:733)
09-22 14:12:21.682: E/WindowManager(1772):  at android.os.Handler.dispatchMessage(Handler.java:95)
09-22 14:12:21.682: E/WindowManager(1772):  at android.os.Looper.loop(Looper.java:136)
09-22 14:12:21.682: E/WindowManager(1772):  at android.app.ActivityThread.main(ActivityThread.java:5001)
09-22 14:12:21.682: E/WindowManager(1772):  at java.lang.reflect.Method.invokeNative(Native Method)
09-22 14:12:21.682: E/WindowManager(1772):  at java.lang.reflect.Method.invoke(Method.java:515)
09-22 14:12:21.682: E/WindowManager(1772):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-22 14:12:21.682: E/WindowManager(1772):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-22 14:12:21.682: E/WindowManager(1772):  at dalvik.system.NativeStart.main(Native Method)

我所发布的代码块中包含的行。如果我检查活动是否为null或者对话框是否为空,那么代码是否可以不破坏?我搞不清楚了。欢迎任何想法。谢谢。

1 个答案:

答案 0 :(得分:1)

只需致电dialog.dismiss(),它就能正常工作......:)