Android中的插页式广告

时间:2014-05-14 16:16:08

标签: android ads interstitial

我完成了我的第一个应用,我想尝试添加一些广告。 我尝试了谷歌提供的教程,他们工作正常。 现在,当我尝试在我的应用程序中实现添加时,我收到错误消息: " IllegalStateException:必须在主UI线程上调用isLoaded"。

所以这就是我在主要活动中所做的,我有onCreate方法:

public void onCreate(){
    // Create the interstitial.
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(MY_AD_UNIT_ID);

    // Create ad request.
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("xxxxxxxxxxxxxx")
    .build();

    // Begin loading your interstitial.
    interstitial.loadAd(adRequest);
}

在同一个活动中我有:

    public void showInterstitial() {
    if (interstitial.isLoaded()) {
        interstitial.show();
        }
    }

因此,当我尝试调用showInterstitial方法时,我收到上述错误。 我现在坐在这几个小时,找不到任何解决办法。

1 个答案:

答案 0 :(得分:0)

我认为以下代码将有助于解决您的问题。在你的on create function

中使用它
interstitial.setAdListener(new AdListener() {
    public void onAdLoaded() {
        showInterstitial();
    }
});    

或者您可以使用以下代码&你可以在设备上测试它。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("***********");

AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
    displayInterstitial();
}
});
}

public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}