现在的位置: 首页 > 综合 > 正文

Error: WebView.destroy() called while still attached 的解决办法

2018年02月14日 ⁄ 综合 ⁄ 共 1220字 ⁄ 字号 评论关闭

今天在改Webview页面的时候出现了闪退。看Log发现是这个问题Error: WebView.destroy() called while still attached。

谷歌了一下。发现只有stackoverflow解释这个问题比较清楚,另外鄙视下百度,啥都搜不到。不过一些问题能用中文描述的,还是百度相关内容比较多。

现在总结下这个问题的解决办法。先大概解释下这个错误,这个错误从字面意思来说是当你结束webview的时候,Webview还依附在父控件下,使用解决这个问题

就是在WebView.destroy()前要解除他们之间的依附关系。一般会碰上这个问题应该是这样对webview进行了操作:

protected void onDestroy()
{
    if (adView != null)
    {
        adView.destroy();
    }
}

附上一段详细的正确代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webviewRelativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/headerAlarmsWebViewTxt"
    android:layout_marginBottom="0dip"
    android:hapticFeedbackEnabled="true"
    android:overScrollMode="never"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:scrollbars="none" />

 </RelativeLayout>
Then you assign it to an instance variable e.g. :

_layout = (RelativeLayout) findViewById(R.id.webviewRelativeLayout);
webView = (WebView) findViewById(R.id.webView1);
and on Destroy do something like this:

@Override
protected void onDestroy() {
    super.onDestroy();
    _layout.removeView(webView);
    webView.removeAllViews();
    webView.destroy();
}

一般看了这段代码就能明白问题所在了。

抱歉!评论已关闭.