54

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.

2
  • Can you post your logcat with exception? Commented Apr 28, 2014 at 10:10
  • Are you using AdMob from Google Play services? Commented Apr 28, 2014 at 10:23

9 Answers 9

180

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

Sign up to request clarification or add additional context in comments.

1 Comment

how to add AdUnit programatically or without use of xml ?
31

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

1 Comment

That was the problem in my case. I had set ID from code, and size from XML.
5

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

1 Comment

Did this help at all?
4

A lot of people Migrating existing applications to the google play services method are incorrectly adding the wrong information.

Something that I would like to point out is that many people with the same question have been using this as a template

<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/lib-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="50dp" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX" ads:loadAdOnCreate="true" > </com.google.android.gms.ads.AdView> 

This is incorrect as xmlns:ads should be change to:- xmlns:ads="http://schemas.android.com/apk/res-auto"

Like so

<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="50dp" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX" ads:loadAdOnCreate="true" > </com.google.android.gms.ads.AdView> 

1 Comment

You have made my day, literally. Changing the namespace was the thing!
2

remove xmlns:ads="http://schemas.android.com/tools" from root view

then add AdView

<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView> 

Remember these steps:

  1. You can define xmlns:ads="http://schemas.android.com/apk/res-auto" on root view it self .

  2. if you have not defined xmlns:ads="http://schemas.android.com/apk/res-auto" inside rootView then you must need to add xmlns:ads="http://schemas.android.com/apk/res-auto" inside AdView.

  3. if you have defined adSize inside your AdView xml file then don't set dynamically, like : adView.setAdSize(AdSize.BANNER); or else it will through an exception java.lang.IllegalStateException: The ad size can only be set once on AdView.

  4. xmlns:ads="http://schemas.android.com/apk/res-auto" and xmlns:app="http://schemas.android.com/apk/res-auto" are looking same but there is a difference which is xmlns:ads and xmlns:app so don't get confuse.

I think it will resolve your issue ... Happy to help :)

Comments

2

I faced that same problem because i do not add this line on xmlns:ads="http://schemas.android.com/apk/res-auto" top of . Then i change my xml code like this

 <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:layout_constraintLeft_toLeftOf="parent" ads:layout_constraintRight_toRightOf="parent" ads:layout_constraintTop_toTopOf="parent" ads:layout_constraintBottom_toTopOf="@+id/headerText" android:layout_marginTop="@dimen/_2sdp" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111" android:visibility="visible"> </com.google.android.gms.ads.AdView> 

after that i add this 3 line in onCreate method

AdView mAdView = (AdView)findViewById(R.id.adView); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); 

Comments

2

you have to replace the xmlns:ads in your parent layout

 xmlns:ads="http://schemas.android.com/tools" **by this** xmlns:ads="http://schemas.android.com/apk/res-auto" 

Comments

1

For anyone interested here is a Kotlin version.

<RelativeLayout android:id="@+id/adContainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true"> </RelativeLayout> 

And inside the code behind

//create a new AdView val adView = AdView(this) adView.adSize = AdSize.SMART_BANNER adView.adUnitId = "your adUnitID goes here or you can use a function" adView.visibility = View.GONE //add it to the layout val layout = adContainer layout.addView(adView) //profit MobileAds.initialize(this) {} val adRequest = AdRequest.Builder().build() adView.loadAd(adRequest) adView.adListener = object : AdListener(){ override fun onAdLoaded() { adView.visibility = View.VISIBLE super.onAdLoaded() } } 

Comments

0

I tried changing the namespace declaration but it just doesn't work. I fixed this by specifying a height value for the adView instead of wrap_content. It works for me

<com.google.android.gms.ads.AdView android:id="@+id/adView2" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_gravity="center|bottom" ads:adSize="BANNER" ads:adUnitId="@string/banner_ad_unit_id" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.