I am getting this error when the device changes orientation:
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?
You first need to detach the Webview:
webViewPlaceholder.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
That did it for me.
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();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mWebView != null) {
mWebView.destroy();
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With