【问题标题】:Interstitial Ads in AndroidAndroid 中的插页式广告
【发布时间】:2014-05-14 16:16:08
【问题描述】:

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

所以这是我在主活动中所做的,我有 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 方法时,我收到了上述错误。 我已经坐了几个小时了,找不到任何解决方案。

【问题讨论】:

    标签: android ads interstitial


    【解决方案1】:

    我认为以下代码将有助于解决您的问题。在你的 on create 函数中使用它

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多