Firona
Firona

Reputation: 67

AdMob error when loading interstitial in emulator

When I try to load an interstitial, I get the following error:

02-25 10:24:58.198    2310-2310/template.maintemplate E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: template.maintemplate, PID: 2310
java.lang.RuntimeException: Unable to start activity ComponentInfo{template.maintemplate/template.maintemplate.sover}: java.lang.IllegalStateException: The ad unit ID must be set on InterstitialAd before loadAd is called.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.IllegalStateException: The ad unit ID must be set on InterstitialAd before loadAd is called.
        at com.google.android.gms.internal.bi.w(Unknown Source)
        at com.google.android.gms.internal.bi.v(Unknown Source)
        at com.google.android.gms.internal.bi.a(Unknown Source)
        at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
        at template.maintemplate.sover.onCreate(sover.java:97)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

The code I used to request an interstitial is directly beneath the code I use to request a banner (which works):

// Ads...
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// Create the interstitial.
interstitial = new InterstitialAd(this);
//interstitial.setAdUnitId(AdRequest.DEVICE_ID_EMULATOR);
// Create ad request.
AdRequest adRequestI = new AdRequest.Builder().addTestDevice("B3EEABB8EE11C2BE770B684D95219").build();
// Begin loading your interstitial.
interstitial.loadAd(adRequestI);

Line 97 is the last line in the example above. I've tried using interstitial.setAdUnitId(AdRequest.DEVICE_ID_EMULATOR) instead of addTestDevice which doesn't throw an error, but also doesn't load the interstitial.

Upvotes: 1

Views: 2525

Answers (2)

Niranj Patel
Niranj Patel

Reputation: 33238

You must have to set AdUnitId before load Ads

    mAdView = (AdView)findViewById(R.id.adView);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("ca-app-pub-160126019342****/405183****");
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
    adRequestBuilder.addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB");

    mAdView.loadAd(adRequestBuilder.build());

Upvotes: 0

nano_nano
nano_nano

Reputation: 12523

you load your Ad before setting the ID:

AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);// <-- Here 

remove the first call so you get:

// Create the interstitial.
interstitial = new InterstitialAd(this);
//interstitial.setAdUnitId(AdRequest.DEVICE_ID_EMULATOR);
// Create ad request.
AdRequest adRequestI = new AdRequest.Builder().addTestDevice("B3EEABB8EE11C2BE770B684D95219").build();
// Begin loading your interstitial.
interstitial.loadAd(adRequestI);

furthermore ad this method:

// Invoke displayInterstitial() when you are ready to display an interstitial.
  public void displayInterstitial() {
    if (interstitial.isLoaded()) {
      interstitial.show();
    }
  }

Upvotes: 3

Related Questions