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

今天在改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();
}


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


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值