Admob插页式广告(全屏)不会显示

时间:2015-12-23 08:00:04

标签: android admob

我想在我的Android应用中显示全屏横幅。

onCreate我称这个函数为:

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

    showInterstitial();
}

我的功能:

private void showInterstitial() {

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(getString(R.string.ad_banner_id));
    interstitialAd.show();

    Toast.makeText(this, "Ad will load", Toast.LENGTH_SHORT).show();
}

我的应用程序将崩溃此消息:

  

引起:java.lang.IllegalStateException:必须设置广告单元ID   在show之前调用InterstitialAd。

但是我在show之前设置了广告ID,不是吗?

3 个答案:

答案 0 :(得分:9)

您没有为interstitialAd致电interstitialAd.loadAd(adRequest); 。插页式广告应在您展示之前加载。

show()

你也应该在调用if(mInterstitial.isLoaded()){ mInterstitial.show(); AdRequest adRequest = new AdRequest.Builder().build(); mInterstitial.loadAd(adRequest); //optionally load again if you plan to show another one } 之前检查它是否已加载。它可能无法立即使用,您可能希望在调用show之前提前加载它。

onCreate()

可能的实施(根据您的要求进行更改)

所以基本上以下内容可以放在 interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(getString(R.string.ad_banner_id)); AdRequest adRequest = new AdRequest.Builder().build(); interstitialAd.loadAd(adRequest); Toast.makeText(this, "Ad will load", Toast.LENGTH_SHORT).show();

showInterstitial()

private void showInterstitial() { if(mInterstitial.isLoaded()){ mInterstitial.show(); //optionally load again if you plan to show another one later AdRequest adRequest = new AdRequest.Builder().build(); mInterstitial.loadAd(adRequest); } } 成为此

showInterstitial()

注意:如果要显示插页式广告,请致电loadAd()。但是,在致电 For eg: if you search GOOGLE it shows About 11,01,00,00,000 results (1.25 seconds),GMAIL About 1,06,00,00,000 results (0.63 seconds). 后不是立即。加载interitital广告需要一些时间,如果网络滞后或广告内容比正常情况重,您可能会错过几分之一秒。

此外,这里是正确实施Example with bullets outside的文档。

答案 1 :(得分:1)

com.google.android.gms.ads.AdView

中添加以下参数
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

确认您在父布局中使用了以下行

xmlns:ads="http://schemas.android.com/apk/res-auto"

just verify code

答案 2 :(得分:1)

public class LoadAd {

    private final AdView ad;

    InterstitialAd interstitialAd;
    boolean isInterstitialShown = false;

    public LoadAd(Context con)
    {
        if (con.getClass().getName() == MainActivity.class.getName())
        {
            interstitialAd = new InterstitialAd(con);
            interstitialAd.setAdUnitId("ca-app-pub-8037008543529602/2054128571");

            AdRequest adRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(adRequest);

            interstitialAd.setAdListener(new AdListener()
            {
                @Override
                public void onAdLoaded()
                {
                    super.onAdLoaded();
                }

                @Override
                public void onAdOpened()
                {
                    super.onAdOpened();
                    isInterstitialShown = true;
                }


                @Override
                public void onAdFailedToLoad(int errorCode)
                {
                    super.onAdFailedToLoad(errorCode);
                }
            });

        }

        ad = (AdView) ((Activity)con).findViewById(R.id.adView);
    }


    public void init()
    {
        if (ad != null)
        {
            AdRequest req = new AdRequest.Builder().build();
            ad.loadAd(req);
        }
    }


    public void destroy()
    {
        if (ad != null)
        {
            ad.destroy();
        }
    }


    public boolean showInterstetial()
    {
        if (isInterstitialShown)
        {
            return false;
        }
        if (interstitialAd != null)
        {
            if (interstitialAd.isLoaded())
            {
                interstitialAd.show();
            }
            else
            {
                return false;
            }
            return true;
        }
        return false;
    }
}
相关问题