首先,对不起我的英语不好。
当我构建apk并尝试在手机中打开apk时,会出现apk has stoped working. The ad unit ID must be set on InterstitialAd before loadAd is called
错误。
我在做什么错?谢谢。
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
MobileAds.initialize(this, "ca-app-pub-9823610014389662~4002090629");
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId("ca-app-pub-9823610014389662/2024107626");
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
}
public void displayInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
答案 0 :(得分:0)
如果logcat
指出问题仅是由于非页内广告引起的,请尝试以下操作:
更简单且经过测试的方式-可以满足95-98%的广告请求
InterstitialAd interstitialAd = null;
然后将其放入onCreate()
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.your_interstitial_string_name);
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
我更喜欢将其命名为onBackPressed()
,您可以根据需要对其进行调整。
@Override
public void onBackPressed() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
}
});
} else {
super.onBackPressed();
}
}
此外,请不要忘记将字符串值放入strings.xml
文件中
<string name="your_interstitial_string_name">ca-app-pub-9823610014389662/2024107626</string>