【问题标题】:The InterstitialAd is not shown in the App, Logcat says: The interstitial ad wasn't ready yet应用程序中未显示插页式广告,Logcat 说:插页式广告尚未准备好
【发布时间】:2022-01-26 00:06:24
【问题描述】:

应用程序正常启动和运行,但未显示 InterstitialAd。 build.gradles 没问题 AndroidManifest 似乎还可以 我之前用 On Start App Ad 尝试过,它没有任何问题,但在这里它不起作用。

我制作了视频教程中的所有内容: https://www.youtube.com/watch?v=UTY5gKmhqxU&t=4s 除了 FullScreenContentCallback(){ 我放入了 InterstitialAd.load 函数。 否则应用程序崩溃。

有人知道为什么 InterstitialAd 没有展示吗? 谢谢你

这是我的代码:

import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends AppCompatActivity implements LocationListener {

  private InterstitialAd mInterstitialAd; //Variable AdMob

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//AdMob
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });


        AdRequest adRequest = new AdRequest.Builder().build();
//ca-app-pub-3940256099942544/1033173712
        InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                        //Log.i(TAG, "onAdLoaded");
                        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Called when fullscreen content is dismissed.
                                Log.d("TAG", "The ad was dismissed.");
                            }

                            @Override
                            public void onAdFailedToShowFullScreenContent(AdError adError) {
                                // Called when fullscreen content failed to show.
                                Log.d("TAG", "The ad failed to show.");
                            }

                            @Override
                            public void onAdShowedFullScreenContent() {
                                // Called when fullscreen content is shown.
                                // Make sure to set your reference to null so you don't
                                // show it a second time.
                                mInterstitialAd = null;
                                Log.d("TAG", "The ad was shown.");
                            }
                        });
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        //Log.i(TAG, loadAdError.getMessage());
                        mInterstitialAd = null;
                    }
                });

        if (mInterstitialAd != null) {
            mInterstitialAd.show(MainActivity.this);
        } else {
            Log.d("TAG", "The interstitial ad wasn't ready yet.");
        }
}

Mainfest 文件:

<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>

【问题讨论】:

  • 您正在尝试在调用 load 后立即展示广告,这显然无法正常工作,因为广告尚未加载。如果您想在广告加载后立即显示广告,请在 onAdLoaded 中使用 show(...)
  • 感谢您的快速回答,现在它可以工作了

标签: java android


【解决方案1】:

广告加载成功后可以展示,如果广告没有加载则不能展示,所以必须在展示广告之前加载。

用这个更新你的代码。

import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
    
public class MainActivity extends AppCompatActivity implements LocationListener {
    
                private InterstitialAd mInterstitialAd; //Variable AdMob
    
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
    //AdMob
                    MobileAds.initialize(this, new OnInitializationCompleteListener() {
                        @Override
                        public void onInitializationComplete(InitializationStatus initializationStatus) {
                        }
                    });
    
    
                    AdRequest adRequest = new AdRequest.Builder().build();
    //ca-app-pub-3940256099942544/1033173712
                    InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
                            new InterstitialAdLoadCallback() {
                                @Override
                                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                    // The mInterstitialAd reference will be null until
                                    // an ad is loaded.
                                    mInterstitialAd = interstitialAd;
                                    //Log.i(TAG, "onAdLoaded");
                                    mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                                        @Override
                                        public void onAdDismissedFullScreenContent() {
                                            // Called when fullscreen content is dismissed.
                                            Log.d("TAG", "The ad was dismissed.");
                                        }
    
                                        @Override
                                        public void onAdFailedToShowFullScreenContent(AdError adError) {
                                            // Called when fullscreen content failed to show.
                                            Log.d("TAG", "The ad failed to show.");
                                        }
    
                                        @Override
                                        public void onAdShowedFullScreenContent() {
                                            // Called when fullscreen content is shown.
                                            // Make sure to set your reference to null so you don't
                                            // show it a second time.
                                            mInterstitialAd = null;
                                            Log.d("TAG", "The ad was shown.");
                                        }
                                    });
    
                                    if (mInterstitialAd != null) {
                                        mInterstitialAd.show(MainActivity.this);
                                    } else {
                                        Log.d("TAG", "The interstitial ad wasn't ready yet.");
                                    }
                                }
    
                                @Override
                                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                    // Handle the error
                                    //Log.i(TAG, loadAdError.getMessage());
                                    mInterstitialAd = null;
                                }
                            });
    
    
                }
            }

【讨论】:

  • 感谢您的快速回答,现在它可以工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多