我集成了 google admob 广告,但显示错误,
W/Ads:获取广告响应时出现问题。错误代码:0 08-09 16:05:43.933 24002-24002/com.locationtracking W/Ads:加载广告失败:0
我创建了一个新的广告 ID 并复制了插页式广告单元 ID 和应用程序 ID,但它不起作用。
但是当我使用 testing interstial id,它正在显示一个广告。但是当我复制并使用创建的插页式广告 ID 时,它不会显示广告。在过去的几天里,我在这方面苦苦挣扎。我创建了新帐户并检查了。仍然是同样的错误。
MobileAds.initialize(this, "ca-app-pub-9172823052987868~3234462570");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-9172823052987868/8576265071");
AdRequest adRequestInterstitial = new AdRequest.Builder().addTestDevice("deviceid").build();
mInterstitialAd.loadAd(adRequestInterstitial);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
}
@Override
public void onAdLoaded() {
mAdIsLoading = false;
showInterstitial();
}
@Override
public void onAdFailedToLoad(int errorCode) {
mAdIsLoading = false;
}
});
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
AdRequest adRequest = new AdRequest.Builder().addTestDevice("deviceid").build();
mInterstitialAd.loadAd(adRequest);
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services-ads:9.2.1'
compile 'com.google.android.gms:play-services-analytics:9.2.1'
}
最佳答案
常见错误!您的代码没有错误。一切都很好。
您只需要稍等一下。您的广告 ID 是新创建的,因此从谷歌服务器获取广告需要一些时间。您可以通过添加您为早期应用程序创建的横幅/插页式广告 ID 来验证这一点,您会看到它们有效。所以给它一些时间,它很快就会起作用。至少对我来说,它一直在发生。
W/Ads:获取广告响应时出现问题。错误代码:0 08-09 16:05:43.933 24002-24002/com.locationtracking W/Ads:加载广告失败:0
删除 addTestDevice("deviceid");从您的代码显示真实广告
AdRequest adRequestInterstitial = new AdRequest.Builder().addTestDevice("deviceid").build();
到
AdRequest adRequestInterstitial = new AdRequest.Builder().build();
尝试下面的代码运行良好:
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////////////////////// banner/////////////////////////
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder().addTestDevice("7339C0A9091FA2D6AA9A2BF29077B5EA").build();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
///////////////////////////////////////////////////////////
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
loadAdd();
}
});
loadAdd();
}
@Override
protected void onStart() {
super.onStart();
showInterstitial();
}
@Override
protected void onStop() {
super.onStop();
showInterstitial();
}
//////////////////////////////////add Interstitial section//////////////////////////////////////////////
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
//Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
loadAdd();
}
}
private void loadAdd() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
//AdRequest adRequest = new AdRequest.Builder().addTestDevice("7339C0A9091FA2D6AA9A2BF29077B5EA").build();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
编译 'com.google.android.gms:play-services-ads:8.3.0'
关于android - 无法显示广告 ErrorCode : 0 in ads android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45588566/