Android 错误:WebView.destroy() 在仍然连接时调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11995270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: WebView.destroy() called while still attached
提问by hellowill89
I am getting this error when the device changes orientation:
当设备改变方向时,我收到此错误:
Error: WebView.destroy() called while still attached
Error: WebView.destroy() called while still attached
With this code:
使用此代码:
protected void onDestroy()
{
if (adView != null)
{
adView.destroy();
}
}
What is the reason for this? How do I avoid this error?
这是什么原因?如何避免此错误?
回答by user1668939
You first need to detach the Webview:
您首先需要分离 Webview:
webViewPlaceholder.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
That did it for me.
那是为我做的。
回答by Matthew
To avoid the error you just need to remove all views before you destroy the ad.
为避免该错误,您只需在销毁广告之前删除所有视图。
@Override
public void onDestroy()
{
if (adView != null)
{
adView.removeAllViews();
adView.destroy();
}
super.onDestroy();
}
回答by Meng
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mWebView != null) {
mWebView.destroy();
}
}
回答by édouard Mercier
According to my tests, this issue is revealed in AdMob SDK v6.4.1 and at least on Android v4.2.2+. When testing the AdMob sample application referred to at https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android(direct link is http://google-mobile-dev.googlecode.com/files/Android_XML.zip), the issue occurs when closing the sample screen.
根据我的测试,这个问题在 AdMob SDK v6.4.1 中出现,至少在 Android v4.2.2+ 上。在测试https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android(直接链接是http://google-mobile-dev.googlecode.com/ files/Android_XML.zip),关闭示例屏幕时会出现此问题。
My work-around is rather:
我的解决方法是:
@Override
public void onDestroy()
{
// Destroy the AdView.
if (adView != null)
{
final ViewGroup viewGroup = (ViewGroup) adView.getParent();
if (viewGroup != null)
{
viewGroup.removeView(adView);
}
adView.destroy();
}
super.onDestroy();
}
Hope that helps other people, and that the AdMob will very soon fix that annoying issue.
希望对其他人有所帮助,并且 AdMob 将很快解决这个烦人的问题。
回答by Klaus Villaca
For you don't get this error you need to have a parent layout, e.g. : RelativeLayout and remove the WebView component, that might had been defined on you layoutWebView.xml.
因为您没有收到此错误,您需要有一个父布局,例如:RelativeLayout 并删除可能已在您的 layoutWebView.xml 上定义的 WebView 组件。
<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:
在 Destroy 上做这样的事情:
@Override
protected void onDestroy() {
super.onDestroy();
_layout.removeView(webView);
webView.setFocusable(true);
webView.removeAllViews();
webView.clearHistory();
webView.destroy();
}