Question

I'm creating my AdView like this in onPostCreate:

 private AdView mAd;

 public void onPostCreate(Activity a) {
     LOGE(TAG, "Ads are enabled: creating");
     mAd = (AdView) (a.findViewById(R.id.adView));
     AdRequest adRequest = new AdRequest();
     adRequest.addTestDevice("xxxxxx");
     mAd.loadAd(new AdRequest());
 }

I'm then destroying it like this:

public void onDestroy(Activity a) {
    if (mAd != null) {
         mAd.destroy();
    }
    super.onDestroy();

}

From my users logs I can see sometimes:

Caused by: java.lang.NullPointerException at android.webkit.WebView.requestFocus(WebView.java:8925) ... at android.view.ViewGroup.removeView(ViewGroup.java:2135) ... at org.akul.psy.gui.BaseActivity.onDestroy(BaseActivity.java:69)

I believe this is because there's some problem with me destroying my AdView.

Do I do that correctly? Do I have to do that at all? I'm destroying it because I've followed Google example (I believe).

Was it helpful?

Solution

You have to destroy the adView only if you have created the AdView instance in code like this

AdView adView = new AdView(this);

but in your case your Adview is in your xml, and your trying to find the instance of the view ( not a new memory allocation). The instance created in xml will be destroyed when the activity is removed from stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top