android - 缺少必需的 XML 属性 "adSize"

标签 android admob

我在启动应用程序时收到“缺少必需的 XML 属性“adSize””。这写在我的智能手机屏幕上,而不是我的横幅上。

我尝试了基于堆栈的不同解决方案(例如 xmlns:ads="http://schemas.android.com/apk/res-auto" 而不是 xmlns:ads= "http://schemas.android.com/apk/libs/com.google.ads" 或代替 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads") 但它不起作用。

这是我的 java 代码(一个简单的 Hello World,只是为了尝试实现一个横幅):

package com.example.publicite;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

这是我的 xml 文件:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

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

<Button
    android:id="@+id/votrescore"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="..." />

</RelativeLayout>

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.publicite"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="13" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

    <activity
        android:name="com.example.publicite.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 </manifest>

我使用 google play 服务库。

最佳答案

尝试以这种方式更改您的声明:

<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"
    ads:adUnitId="YOUR_AD_UNIT_ID"
    ads:adSize="BANNER"/>

---- 编辑----

您也可以尝试以编程方式进行。简单地说,代替 AdView 布局项,定义一个 LinearLayout 并通过代码执行如下操作:

final AdView adView = new AdView(getActivity());
adView.setAdUnitId("YourId");
adView.setAdSize(AdSize.BANNER);

final LinearLayout adLinLay = (LinearLayout) view.findViewById(R.id.your_defined_linearlayout);
adLinLay.addView(adView);

final AdRequest.Builder adReq = new AdRequest.Builder();
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);

关于android - 缺少必需的 XML 属性 "adSize",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22673111/

相关文章:

android - Android 中带改造的简单类型 Joda 日期时间

iphone - 如何让Admob显示在iPhone屏幕顶部?

java - 使用 Google Admob 自动滚动 fragment 和 Activity ?

android - RecyclerView 和 NestedScrollView 底部的 Admob

java - Android 自定义 ListView 仅显示 Firebase 数据库中的一项

android - 有没有办法在不使用外部库的情况下将数据插入 couch base lite?

java - 从 Android 调用 Soap Web 服务

php - header (位置 : . ..)在 Android 上的 webview 中不完全工作

android - Google Play 商店说我的应用程序使用 AdMob

android - Admob 插页式 loadEd 功能太慢