Admob插屏广告(全屏)无法显示

8
我可以帮您翻译成中文,如下:

我想在我的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: 必须在展示InterstitialAd之前设置广告单元ID。

但是我已经在展示之前设置了广告ID,是吗?


你是否在资源文件(如string.xml)中添加了banner_ad_unit_id? - Dhaval Parmar
是的,如果我使用Log.d输出这个id,它将会在log cat中显示。 我直接把id放进测试中:interstitialAd.setAdUnitId("xxxxx"); 但是出现了同样的错误。 - SpecialFighter
@DhawalSodhaParmar 这并不重要。即使是横幅广告单元ID也可以获取插页式广告。虽然它可以工作,但不建议这样做。 - Viral Patel
3个回答

10
你没有调用loadAd()来加载插页式广告。在显示之前,插页式广告应该被先加载。
interstitialAd.loadAd(adRequest);

在调用 show() 之前,您应该检查它是否被加载。它可能不会立即可用,您可能希望在调用 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
        }

可能的实现(根据您的需求进行更改)

基本上以下内容可以放在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()方法后不要立即调用它。由于插页式广告需要一段时间来加载,如果网络延迟或广告内容较重,你可能会错过机会。
此外,这里有实现Admob插页式广告的正确方式的文档。

顺便说一句,在应用程序加载时显示插页式广告是不建议的,也违反了指南。您应该在某些操作之后或退出之前显示它。基本上在应用程序使用后的某个逻辑点上显示。 - Viral Patel
然后通过重写“onAdLoaded()”方法立即在加载后显示。您需要创建一个新的AdListener,然后覆盖此方法。但是,如我之前所说,不建议这样做。 - Viral Patel
好的,那我就不这样做了。但是如果我在操作栏项目上单击后显示此广告-第一次将显示横幅。但是如果我回到主活动并再次触摸此蝙蝠项目-不会显示横幅。为什么? - SpecialFighter
我认为你应该尝试调试一下。 :) - Viral Patel
我像专业人士一样调试代码!xD - 但是我不理解这个。 - SpecialFighter
显示剩余9条评论

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"

只需验证代码


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接