Exception-WebView.destroy() called while still attached

Exception

1
2
webview: java.lang.Throwable: Error: WebView.destroy() called while still attached!at android.webkit.WebViewClassic.destroy(WebViewClassic.java:2203)
at android.webkit.WebView.destroy(WebView.java:683)

拥有webview的Activity在finish()时,会有异常输出:WebView.destroy() called while still attached!

原因

原因是因为在webview执行destory()时,改webview还依附在其父控件上,需要将其 detach掉。

解决

解决方案是可以先将该webview从其父控件中的detach掉,即revome掉。

1
2
3
4
parentLayout.removeView(mWebView);
mWebView.removeAllViews();
mWebView.destroy();
//如果有需要,最好再判空一下

Fork me on GitHub