Admob ad will not load. Error 2. It worked before but suddenly stopped

后端 未结 12 1850
长情又很酷
长情又很酷 2020-12-11 01:29

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         


        
12条回答
  •  一个人的身影
    2020-12-11 01:50

    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.

提交回复
热议问题