在模拟器中加载插页式广告时出现 AdMob 错误

AdMob error when loading interstitial in emulator

当我尝试加载插页式广告时,出现以下错误:

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.access0(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.access0(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)

我用来请求插页式广告的代码就在我用来请求横幅广告(有效)的代码下方:

// 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);

第 97 行是上例中的最后一行。我试过使用 interstitial.setAdUnitId(AdRequest.DEVICE_ID_EMULATOR) 而不是 addTestDevice,它不会引发错误,但也不会加载插页式广告。

您在设置 ID 之前加载您的广告:

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

删除第一个调用,这样你会得到:

// 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);

此外还有这个方法:

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

您必须在加载前设置 AdUnitId 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());