Here is the entire class that I am trying to load a banner ad into (createAd() method does the work)
public class HomeActivity extends Activity {
privat
at first be clear about which type of add you want to load, interstitial or banner ad. if you want to load banner ad, create a banner ad_unit_id on admob site, then use AdView directly inside your activity view as below:
and just request to load banner ad from your code as below: write this code in your activity inside oncreate after setcontentview
AdView mAdView = (AdView) findViewById(R.id.xadView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
AdRequest.DEVICE_ID_EMULATOR).build();
mAdView.loadAd(adRequest);
AND
if you want to load interstial ads, create a interstitial ad_unit_id on admob site, then use it in below method:
public void showFullScreenAd() {
try {
com.google.android.gms.ads.InterstitialAd interstitial = new com.google.android.gms.ads.InterstitialAd(context);
interstitial
.setAdUnitId(ADMOB_INTERSTITIAL_AD_UNIT_ID);
// Check the logcat output for your hashed device ID to get test ads
// on
// a physical device.
com.google.android.gms.ads.AdRequest adRequest = new AdRequest.Builder()
.build();
// Load the interstitial ad.
interstitial.loadAd(adRequest);
interstitial
.setAdListener(new com.google.android.gms.ads.AdListener() {
@Override
public void onAdLoaded() {
interstitial.show();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
NOTE: important thing is that sometimes we use banner ad unit id inside interstitial ads or interstitial ad unit id inside banner ads, so this error occurs.