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

时间:2015-02-25 10:42:47

标签: java android admob

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

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)

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

// 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,它不会引发错误,但也不会加载插页式广告。

2 个答案:

答案 0 :(得分:3)

在设置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();
    }
  }

答案 1 :(得分:0)

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