Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: WebView.destroy() called while still attached

Tags:

android

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?

like image 499
hellowill89 Avatar asked Aug 16 '12 20:08

hellowill89


4 Answers

You first need to detach the Webview:

webViewPlaceholder.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();

That did it for me.

like image 129
user1668939 Avatar answered Oct 24 '22 03:10

user1668939


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();
}
like image 40
Matthew Avatar answered Oct 24 '22 04:10

Matthew


 @Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (mWebView != null) {
        mWebView.destroy();
    }
}
like image 27
Meng Avatar answered Oct 24 '22 02:10

Meng


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.

like image 20
Édouard Mercier Avatar answered Oct 24 '22 03:10

Édouard Mercier