Search code examples
androidadmobadsinterstitial

Interstitial not loading.?


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);
}

}

My problem is it always shows the toast "Ad failed to Load".I don't know what is missing in my code. Please Help!!. And I have already searched a lot on this so please don't post links to another questions.

Manifest

<?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

http://pastebin.com/w5hDMT3B


Solution

  • Logcat clearly says:

    06-01 00:59:20.883  16251-16326/abc.myapplication I/Ads﹕ No fill from ad server.
    

    That is why you are not getting any ads. There are none available for you at this time, place and device. If you want to test the you need to set the test adUnitId. Which is also given by 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.