Search code examples
androidadmobadsinterstitial

Interstitial and banner ads fail to load


I've been working on this code where apparently the interstitial and banner ads keep failing to load.

Here's the code for creating the banner adview and adding the newly generated ad units. Also the code for interstitial ads which is supported with an adlistener to see if the ads work..

//Ads ----------------
    // Create the adView
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    //<!-- Ads Using Google Play Services SDK -->
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the adView to it
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

    adView.setLayoutParams(params);

    layout.addView(mGLSurfaceView);
    layout.addView(adView);

    setContentView(layout);
    //New AdRequest 
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
    //-----------------------------------------------------Interstitial Add
    // Create an Interstitial ad.

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
    interstitialAd.loadAd(new AdRequest.Builder().build());
    interstitialAd.setAdListener(new AdListener() {
          @Override
          public void onAdLoaded() {
            interstitialAd.show();
          }

          @Override
          public void onAdFailedToLoad(int errorCode) {
              Toast.makeText(getApplicationContext(), "Interstitial Ads loading failed", Toast.LENGTH_SHORT).show();
          }
    });

     // Load the interstitial ad.
    showInterstitialAds();

public void showInterstitialAds()
{
    runOnUiThread(new Runnable() {
        public void run() {
             AdRequest interstitialAdRequest = new AdRequest.Builder().build();
             interstitialAd.loadAd(interstitialAdRequest);
        }
    });
}

In the app, all I get is the Toast message declaring that the interstitial ads don't work. Even the banner ads don't work. On the logcat, I get different messages: I last got:

01-17 16:54:05.765: I/Ads(1136): No fill from ad server.
01-17 16:54:05.769: I/Ads(1136): Scheduling ad refresh 60000 milliseconds from now.
01-17 16:54:05.769: W/Ads(1136): Failed to load ad: 3

and there's this:

01-17 16:52:02.945: I/Ads(1136): Starting ad request.
01-17 16:52:02.945: I/Ads(1136): Use AdRequest.Builder.addTestDevice("A55B0AA295150C0583B3FADA0B02054B") to get test ads on this device.

I tried the above in the showInterstitialAds but I kept receiving that same error and ads didn't show.

Also, the most interesting one is:

There was a problem getting an ad response. ErrorCode: 2

With different numbers on the errocode: 0,2,3. I'm not sure what it means as I'm new to the whole android world (I just started coding a few months ago.)


Solution

  • Banner ads aren't loading because there are no ads available. That's what "No fill from ad server means". It happens occasionally especially when your traffic is low.

    To test you should be configuring your AdView to show test ads. See https://developer.android.com/reference/com/google/android/gms/ads/AdRequest.Builder.html#addTestDevice(java.lang.String)

    Hard to say what is going wrong with your interstitial. Are you using the correct AdUnitId?

    Also never call interstitial#show() from onAdLoaded(). It' provides a very bad user experience and will probably get your account banned.

    Instead you should call interstial#show() from a natural break point in your app, like at the end of a game level.