Android - AdView missing required XML attribute 'adSize'

点点圈 提交于 2019-12-18 14:45:50

问题


My head almost exploded today because the entire day I tried to solve the problem with my AdView from AdMob. I'm getting an error mentioned in the title. I googled through 3 pages but nothing. Same error whatever I do. This is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.48"
        android:src="@drawable/butters10" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="42dp" >

        <Button
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/prev" />

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/next" />
    </RelativeLayout>

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:loadAdOnCreate="true"
        app:adSize="BANNER"
        app:adUnitId="------------------------------" >
    </com.google.ads.AdView>

</LinearLayout>

I tried to change the app keyword to ads but the problem still shows up.

Another thing is I actually want to add the same ad this the same unit-id to more than one activity. I added this exactly code to my first activity xml file, and everything works fine, but in the second activity it doesn't work at all. Thank you for any help.


回答1:


Change

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

to

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

All the documentation seems to refer to the namespace as being at /lib/ not /libs/

And reference the tags using ads

<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:loadAdOnCreate="true"
    ads:adSize="BANNER"
    ads:adUnitId="------------------------------" />



回答2:


To make it work with Google Play services use:

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

in your XML file.




回答3:


My requirement was to be able to dynamically pass in ad unit IDs instead of specifying them in xml. Turns out the framework does not like a mix of these, so I am creating my ads 100% programmatically:

public class AdMobBanner extends FrameLayout {

private AdView adView;

public AdMobBanner(Context context, AdParams params) {
    super(context);

    adView = new AdView(context);
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER;
    adView.setLayoutParams(layoutParams);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(params.getAdUnitId());
    addView(adView);
}

public void showAdvert(AdParams params) {
    AdRequest.Builder builder = new AdRequest.Builder();

    for (String id: params.getTestDevices()) {
        builder.addTestDevice(id);
    }
    adView.loadAd(builder.build());
}

@Override
public void pause() {
    adView.pause();
}

@Override
public void resume() {
    adView.resume();
}

}

The AdParams class is my own class and you can replace it with your own class, or pass in strings, but the general solution is posted here.




回答4:


The "loadAdOnCreate" and "testDevices" XML attributes no longer available. To make your ad working with Google Play Services

Here is the XML layout

<?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>

And here is your java file

package com.google.example.gms.ads.xml;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.app.Activity;
import android.os.Bundle;

/**
 * A simple {@link Activity} which embeds an AdView in its layout XML.
 */
public class BannerXMLSample extends Activity {

  private static final String TEST_DEVICE_ID = "INSERT_YOUR_TEST_DEVICE_ID_HERE";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice(TEST_DEVICE_ID)
        .build();
    adView.loadAd(adRequest);
  }
}

Refer the example here https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/banner-xml




回答5:


Add android:theme="@android:style/Theme.Translucent" to your Android Manifest after meta-data



来源:https://stackoverflow.com/questions/20153270/android-adview-missing-required-xml-attribute-adsize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!