Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have this in my main xml file:

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="******************"
        ads:adSize="BANNER"
        android:id="@+id/adView"/>

I have already set the ad size and unit id, but when this is run (from MainActivity.java),

AdView adView = (AdView)this.findViewById(com.example.lovetestactual.R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

It threw an exception of what is in the title.

like image 737
zbz.lvlv Avatar asked Apr 28 '14 09:04

zbz.lvlv


3 Answers

I found solution in the github example, that is:

instead of

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

delete the xmlns:ads*** tag and add

xmlns:ads="http://schemas.android.com/apk/res-auto"

tag to your LinearLayout tag as follow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"/>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       ads:adSize="BANNER"
                       ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>

That's it :)

github link for this xml

like image 136
Hamed MP Avatar answered Oct 14 '22 16:10

Hamed MP


Warning: Make sure you set the ad size and ad unit ID in the same manner (i.e. set both in XML or both programmatically).

https://developers.google.com/admob/android/banner

like image 28
Dan Alboteanu Avatar answered Oct 14 '22 16:10

Dan Alboteanu


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:

<RelativeLayout
    android:id="@+id/test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/TextView07"
    android:paddingTop="20dp" >
</RelativeLayout>

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

like image 5
furkick Avatar answered Oct 14 '22 16:10

furkick