【问题标题】:Required XML attribute 'adsize' was missing (google play service)缺少必需的 XML 属性“adsize”(谷歌播放服务)
【发布时间】:2014-05-13 11:11:59
【问题描述】:

有很多这样的问题,但不是重复的

因为我已经用过 xmlns:ads="http://schemas.android.com/apk/res-auto" 我主要是在java中编写代码

我想使用谷歌播放服务来投放广告

我用过下面的java代码

    com.google.android.gms.ads.AdView adView = (com.google.android.gms.ads.AdView) activity.findViewById(R.id.adview);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(activity.getString(R.string.admob_id));
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

和下面的xml代码

 <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="wrap_content"
     />

但我在横幅中遇到错误就像

缺少所需的 XML 属性“adsize”

【问题讨论】:

  • 尝试在LinearLayout中添加Adview,先取linearlayout
  • 您在 xml 中缺少 ads:adSize="BANNER" 属性

标签: android admob google-play-services


【解决方案1】:

将命名空间改为

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

Ad.xml

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

    <com.google.android.gms.ads.AdView
        android:id="@+id/adview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER" />

</LinearLayout>

注意:

将此命名空间用于新的 SDK

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

【讨论】:

  • 很好的答案,这真的很难找到。 +1
  • 谢谢,xmlns:ads="schemas.android.com/apk/res-auto" 解决了我的问题。太愚蠢了,AS 没有添加正确的命名空间...
【解决方案2】:

尝试将“ads:adSize”添加到 XML,如下所示:

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adview"
    ads:adSize="SMART_BANNER"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     />

【讨论】:

    【解决方案3】:

    删除“adView.setAdSize(AdSize.SMART_BANNER);”来自你的 java 文件。

    参考以下:

    这是 XML 布局

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

    这是你的 java 文件

    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);
      }
    }
    

    参考这里的例子 https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/banner-xml

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多