Google Admob Ad 在加载插页式广告时崩溃应用
Posted
技术标签:
【中文标题】Google Admob Ad 在加载插页式广告时崩溃应用【英文标题】:Google Admob Ad crashes app while loading interstitial ad 【发布时间】:2021-12-25 16:13:51 【问题描述】:我的应用在加载 Google Admob 广告时崩溃。它工作了很长时间,但现在我收到以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.google.android.gms.ads.interstitial.InterstitialAd.setFullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback)”
我没有更改任何代码,我的应用程序在 Play 商店中,并且我的 Galaxy S10 上有该应用程序并且它一直有效,但现在如果必须加载广告,我会收到此错误。有人可以帮忙吗? 谢谢
编辑:这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener()
@Override
public void onInitializationComplete(InitializationStatus initializationStatus)
);
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-93706071examplenumber/4141725063", adRequest, new InterstitialAdLoadCallback()
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd)
mInterstitialAd = interstitialAd;
Log.i("TAG", "onAdLoaded");
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError)
Log.i("TAG", "onAdLoaded");
mInterstitialAd = null;
);
private void showInterstitial()
if (callbackActive == false)
setCallback();
if (mInterstitialAd != null )
mInterstitialAd.show(MainActivity.this);
else
Log.d("TAG", "The interstitial ad wasn't ready yet.");
private void setCallback()
callbackActive = true;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback()
@Override
public void onAdDismissedFullScreenContent()
Log.d("TAG", "The ad was dismissed.");
@Override
public void onAdFailedToShowFullScreenContent(AdError adError)
Log.d("TAG", "The ad failed to show.");
@Override
public void onAdShowedFullScreenContent()
mInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
);
【问题讨论】:
请添加您用于展示广告的代码 我有问题地添加了它。问候 【参考方案1】:将setFullScreenContentCallback
放入onAdLoaded
这是一个代码 sn-p(参见下面的代码)并查看来自AdMob official sample on GitHub的完整代码
public void loadAd()
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
this,
AD_UNIT_ID,
adRequest,
new InterstitialAdLoadCallback()
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd)
// The mInterstitialAd reference will be null until
// an ad is loaded.
MyActivity.this.interstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
Toast.makeText(MyActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback()
@Override
public void onAdDismissedFullScreenContent()
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
MyActivity.this.interstitialAd = null;
Log.d("TAG", "The ad was dismissed.");
@Override
public void onAdFailedToShowFullScreenContent(AdError adError)
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
MyActivity.this.interstitialAd = null;
Log.d("TAG", "The ad failed to show.");
@Override
public void onAdShowedFullScreenContent()
// Called when fullscreen content is shown.
Log.d("TAG", "The ad was shown.");
);
【讨论】:
@OSbOoL 这有帮助吗?以上是关于Google Admob Ad 在加载插页式广告时崩溃应用的主要内容,如果未能解决你的问题,请参考以下文章
cocos2d-x / firebase / admob无法加载插页式广告