Android Admob 插页式广告不会显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20854789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:35:25  来源:igfitidea点击:

Admob interstitial ad won't display

androidadmobinterstitial

提问by user2661663

I used to display AdMob banner on my future apps, and I'd like to give a try to the interstitial ads.

我曾经在我未来的应用程序中显示 AdMob 横幅,我想尝试一下插页式广告。

I checked the AdMob SDK for implementation, and I copied their example source because it was exactly what I want (i.e. the interstitial shown when the activity launch).

我检查了 AdMob SDK 的实施情况,并复制了他们的示例源,因为这正是我想要的(即活动启动时显示的插页式广告)。

I tried it on emulator and on my Galaxy, no ad has been displayed.

我在模拟器和我的 Galaxy 上尝试过,没有显示任何广告。

Here is the source code:

这是源代码:

public class Asscreed extends Activity {
    private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asscreed);

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-6046034785851961/xxxxxx");

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

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

    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

The imports are OK and the Google Play Services library is of course imported.

导入没问题,当然也导入了 Google Play 服务库。

I use this example: AdMob Android Guides - Interstitial Ad.

我使用这个例子: AdMob Android 指南 - 插页式广告

Could someone tell me what's wrong in my code?

有人能告诉我我的代码有什么问题吗?

回答by Kumar Bibek

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

您应该等待广告加载完毕。只有这样,您才能调用displayInterstial()方法,该方法将显示广告。

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

您可以注册一个监听器,它会在加载完成时通知您。

interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});

回答by 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..

我仍然想知道为什么谷歌的所有家伙都上传了代码,这些代码在一个人跟随他们到点之后就不起作用了。

回答by Muhammad Hamza Shahid

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

我有同样的问题。问题在于清单中未定义 admob 活动。请确保您的清单文件中有以下活动标签

<activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

回答by Null Pointer Exception

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

                    }
                });

回答by live-love

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

尝试在 onCreate 中对其进行初始化,但仅从事件(例如按钮单击)中调用 show() 方法。

You can use this code, the test ids work:

您可以使用此代码,测试 ID 有效:

    @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.");
        }
    }

回答by Tsunaze

You didn't call displayInsterstitial().

您没有调用 displayInsterstitial()。

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

但是你应该做的是调用interstitial的侦听器:

interstitial.setAdListener(this);

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

然后它将为您的 Activity 实现侦听器,然后将其显示在 onAdLoaded(或其他内容)上。