Admob interstitial ad won't display

五迷三道 提交于 2019-11-28 09:00:39

You should wait for the ad to be loaded. Only then, you can call displayInterstial() method, which would show the ad.

You can register for a listener, that will let you know when the loading is done.

interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});
Philly Robo-arch Kintu

this did it for me.

// Begin listening to interstitial & show ads.
interstitial.setAdListener(new AdListener(){
     public void onAdLoaded(){
          interstitial.show();
     }
});

i still wonder why all the guys at google upload code implementing directions that just don't work after one follows them to the dot..

I had the same problem. The issue was that admob activity was not defined in manifest. Please make sure you have folllowing activity tag in your manifest file

<activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.setAdUnitId("Your Interstitial Id ca-app-pub-46563563567356235/3452455");
                AdRequest adRequest1 = new AdRequest.Builder()
                        .build();
                mInterstitialAd.loadAd(adRequest1);
                mInterstitialAd.setAdListener(new com.google.android.gms.ads.AdListener() {
                    @Override
                    public void onAdLoaded() {
                        mInterstitialAd.show();
                        super.onAdLoaded();

                    }
                });

Try initializing it in onCreate, but only call the show() method from an event, such as a button click.

You can use this code, the test ids work:

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

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //test id

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //test ad

        mInterstitialAd.loadAd(new AdRequest.Builder().build());


        btn_test.setOnClickListener(view -> {
            showInterstitial();
        });
...
    }    
    private void showInterstitial()
    {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }

You didn't call displayInsterstitial().

But what you should do is call the listener for the interstitial :

interstitial.setAdListener(this);

It will then implements the Listener to your Activity and then display it onAdLoaded (or something).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!