我想在我的
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();
}
我的应用程序将崩溃此消息:
Caused by: java.lang.IllegalStateException: The ad unit ID must be set
on InterstitialAd before show is called.
但是我在演出之前设置了广告ID,不是吗?
解决方法
您没有为interstitialAd调用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,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 Intersitials的文档.