Ad Size and Ad unit id must be set before loadAd is called

后端 未结 8 1488
后悔当初
后悔当初 2020-12-03 00:52

I have this in my main xml file:



        
8条回答
  •  误落风尘
    2020-12-03 01:05

    This is going to be vague as you have not stated what type of view your XML file is (Relative, Linear etc.)

    In my application with a scrollable relative layout I have included:

    
    
    

    Now inside my actual class that I wan't the ad I have included:

    private AdView adView;
    
    /* Your ad unit id. Replace with your actual ad unit id. */
    private static final String AD_UNIT_ID = "ca-app-pub-xxxxxx/yyyyyy";
    
    /**
     * Simply loads the xml about page layout to display the text.
     */
    
    public void onCreate(Bundle start) {
        super.onCreate(start);
        setContentView(R.layout.about);
    
        final TelephonyManager tm = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
    
        //String deviceid = tm.getDeviceId();
    
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
    
        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.test);
        layout.addView(adView);
    
        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
        .build();
                //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                //.addTestDevice(deviceid).build();
    
        // Start loading the ad in the background.
        adView.loadAd(adRequest);
    }
    

    Now ideally you want to use a smart banner to optimise your layout for all devices, as I have above, this is standard now.

    If this is no help head over to here

提交回复
热议问题