デバイスの向きが変わると、次のエラーが発生します。
Error: WebView.destroy() called while still attached
このコードで:
protected void onDestroy()
{
if (adView != null)
{
adView.destroy();
}
}
これの理由は何ですか?このエラーを回避するにはどうすればよいですか?
デバイスの向きが変わると、次のエラーが発生します。
Error: WebView.destroy() called while still attached
このコードで:
protected void onDestroy()
{
if (adView != null)
{
adView.destroy();
}
}
これの理由は何ですか?このエラーを回避するにはどうすればよいですか?
まず、Webviewをデタッチする必要があります。
webViewPlaceholder.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
それは私のためにそれをしました。
エラーを回避するには、広告を破棄する前にすべてのビューを削除する必要があります。
@Override
public void onDestroy()
{
if (adView != null)
{
adView.removeAllViews();
adView.destroy();
}
super.onDestroy();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mWebView != null) {
mWebView.destroy();
}
}
私のテストによると、この問題は 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();
}
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>
次に、それをインスタンス変数に割り当てます。
_layout = (RelativeLayout) findViewById(R.id.webviewRelativeLayout);
webView = (WebView) findViewById(R.id.webView1);
Destroy では、次のようにします。
@Override
protected void onDestroy() {
super.onDestroy();
_layout.removeView(webView);
webView.setFocusable(true);
webView.removeAllViews();
webView.clearHistory();
webView.destroy();
}