缺少必需的XML属性“adsize”(Google Play服务)

时间:2014-05-13 11:11:59

标签: android admob google-play-services

有这样的问题,但它不重复

因为我已经使用过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属性&#39; adsize&#39;失踪

3 个答案:

答案 0 :(得分:4)

将名称空间更改为

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 :(得分:3)

尝试添加&#34;广告:adSize&#34;像下面这样的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"
     />

答案 2 :(得分: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