2014-09-22 68 views
2

從接收活動對象的引用的Java類處理多個webviews時,我的代碼中存在一個錯誤。在Android中有對話框和wevView的泄漏窗口

這是我的代碼:

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) 

被指出存在的線是我張貼的代碼塊中。如果我檢查活動是否爲空或者對話框是否爲空,那麼不應該讓代碼不要中斷嗎?我搞不清楚了。任何想法都歡迎。謝謝。

+1

你關閉你的對話,如果它已經打開時'onDestroy'被稱爲?你應該控制方向的改變,最近發生在我身上 – zozelfelfo 2014-09-22 11:28:09

+0

我的Java類中沒有onDestroy()方法,不幸的是:(我正在處理來自非活動類的這些方法,只有Activity對象的引用 – user1140656 2014-09-22 11:32:40

+1

@ user1140656剛剛在退出活動時添加dialog.dismiss()... – Umair 2014-09-22 11:37:11

回答

1

就叫dialog.dismiss(),它會很好地工作.... :)