当设备更改方向时,我收到此错误:
Error: WebView.destroy() called while still attached
使用此代码:
protected void onDestroy()
{
if (adView != null)
{
adView.destroy();
}
}
Run Code Online (Sandbox Code Playgroud)
这是什么原因?如何避免此错误?
小智 49
首先需要分离Webview:
webViewPlaceholder.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
Run Code Online (Sandbox Code Playgroud)
这样做对我来说.
Mat*_*hew 29
要避免此错误,您只需在销毁广告之前删除所有视图.
@Override
public void onDestroy()
{
if (adView != null)
{
adView.removeAllViews();
adView.destroy();
}
super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)
小智 10
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mWebView != null) {
mWebView.destroy();
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的测试,此问题在AdMob SDK v6.4.1中显示,至少在Android v4.2.2 +上显示.在测试https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android上提到的AdMob示例应用程序时(直接链接是http://google-mobile-dev.googlecode.com/ files/Android_XML.zip),关闭示例屏幕时会出现问题.
我的解决方法是:
@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();
}
Run Code Online (Sandbox Code Playgroud)
希望能够帮助其他人,并且AdMob将很快解决这个恼人的问题.
因为你没有得到这个错误你需要有一个父布局,例如: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>
Run Code Online (Sandbox Code Playgroud)
然后将其分配给实例变量,例如:
_layout = (RelativeLayout) findViewById(R.id.webviewRelativeLayout);
webView = (WebView) findViewById(R.id.webView1);
Run Code Online (Sandbox Code Playgroud)
并在Destroy上做这样的事情:
@Override
protected void onDestroy() {
super.onDestroy();
_layout.removeView(webView);
webView.setFocusable(true);
webView.removeAllViews();
webView.clearHistory();
webView.destroy();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31887 次 |
| 最近记录: |