public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
@Override
public void onBackPressed() {
ShowAd();
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
//mInterstitialAd.setAdUnitId(String.valueOf(R.string.interstitial_ad_unit_id));
//mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
mInterstitialAd.setAdUnitId("ca-app-pub-6342248862135370/7323606247");
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(),"Ad Failed To Load", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLoaded() {
Toast.makeText(getApplicationContext(),"Ad Loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed(){requestNewInterstitial();}
});
//Load Ad Starts
AdView mAdView=(AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//Load Ad Stops
}
public void second(View view){
Intent i = new Intent(getApplicationContext(), second.class);
startActivity(i);
}
public void ShowAd(){
if(mInterstitialAd.isLoaded()){
Toast.makeText(getApplication(), "The Ad has Loaded.", Toast.LENGTH_SHORT).show();
mInterstitialAd.show();
}
else
Toast.makeText(getApplication(), "The Ad has not loaded yet.", Toast.LENGTH_SHORT).show();
}
public void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
我的问题是它始终显示吐司"广告无法加载"。我不知道我的代码中缺少什么。请帮忙!!。我已经对此进行了大量搜索,因此请不要发布其他问题的链接。
清单
<?xml version="1.0" encoding="utf-8"?>
<!-- Include required permissions for Google Mobile Ads to run -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity
android:name=".second"
android:label="@string/title_activity_second"></activity>
<activity
android:name=".third"
android:label="@string/title_activity_third"></activity>
</application>
logcat的
答案 0 :(得分:0)
Logcat清楚地说:
06-01 00:59:20.883 16251-16326/abc.myapplication I/Ads﹕ No fill from ad server.
这就是为什么你没有收到任何广告。目前,地点和设备都没有。如果你想测试你需要设置测试adUnitId。这也是由logcat提供的:
06-01 00:59:20.159 16251-16251/abc.myapplication I/Ads﹕ Use AdRequest.Builder.addTestDevice("1CC3C3CDC4D015059D600FE0D095EAE5") to get test ads on this device.