Android 缺少必需的 XML 属性“adSize”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22673111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Required XML attribute "adSize" was missing
提问by Glork
I get a "Required XML attribute "adSize" was missing" when I launch my app. This is written on the screen of my smartphone instead of my banner.
当我启动我的应用程序时,我得到一个“必需的 XML 属性“adSize”丢失”。这是写在我智能手机的屏幕上,而不是我的横幅上。
I tried the different solutions founded on stack (like xmlns:ads="http://schemas.android.com/apk/res-auto"
instead of xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
or instead of xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
) but it doesn't work.
我尝试了基于堆栈的不同解决方案(例如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"
),但它不起作用。
Here's my java code (a simple Hello World just to try to implement a banner):
这是我的 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;
}
}
Here's my xml file:
这是我的 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>
and here's my manifest:
这是我的清单:
<?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>
I use the google play service lib.
我使用谷歌播放服务库。
采纳答案by zizutg
Here is a weird answer. I just restarted my eclipse and it stared working. This might help some one.
这是一个奇怪的答案。我刚刚重新启动了我的日食,它开始工作了。这可能对某些人有所帮助。
Just restart eclipse or android studio
只需重新启动 eclipse 或 android studio
回答by nKn
Try changing your declaration this way:
尝试以这种方式更改您的声明:
<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"/>
---- EDIT ----
- - 编辑 - -
You can also try to do it programmatically. Simply, instead of an AdView
layout item, define a LinearLayout
and by code do something like this:
您也可以尝试以编程方式进行。简单地说,而不是一个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);
回答by ketom
I have same problem.
我有同样的问题。
Solution for me:
对我的解决方案:
xmlns:ads=""
changed to
变成
xmlns:ads="http://schemas.android.com/apk/res-auto"
回答by joselquin
I had the same problem and I corrected it with the xmlns labels. I put two "xmlns:ads" labels (one for "res-auto" in the AdView and other for "tools" in the parent layout). I had to use two labels for "res auto", one with xmlns:app in the FloatingActionButton and other with xmlns:ads in the AdView:
我遇到了同样的问题,我用 xmlns 标签更正了它。我放置了两个“xmlns:ads”标签(一个用于 AdView 中的“res-auto”,另一个用于父布局中的“工具”)。我不得不为“res auto”使用两个标签,一个在 FloatingActionButton 中使用 xmlns:app,另一个在 AdView 中使用 xmlns:ads:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/fragment_start_margin"
android:background="@drawable/fondo_proyectos"
android:elevation="4dp"
android:gravity="bottom"
android:minHeight="@dimen/toolbar_height">
</android.support.v7.widget.Toolbar>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/av_bottom_banner"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_inferior_ad_unit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/av_bottom_banner"
android:fitsSystemWindows="true">
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/av_bottom_banner"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_marginLeft="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:elevation="4dp"
android:src="@android:drawable/ic_input_add"
android:tint="@android:color/white"
app:fabSize="normal"
app:srcCompat="@drawable/ic_account_circle" />
<include layout="@layout/proyecto_content" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
回答by kartikag01
Problem fixed for me by changing name space uri
通过更改名称空间 uri 为我解决了问题
from
从
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
to
到
xmlns:ads="http://schemas.android.com/apk/res-auto"
回答by Abanoub Hanna
I faced the same error. but I couldn't fix it with the pinned answer. I changed the xml to be like this.
我遇到了同样的错误。但我无法用固定答案修复它。我将xml更改为这样。
<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>
The main changes are:
xmlns:ads="http://schemas.android.com/apk/res-auto"
and
ads:adSize="BANNER"
.
I recorded my laptop screen while solving this issue. So If you like visual illustrations, you can see the video on YouTube.
主要变化是:
xmlns:ads="http://schemas.android.com/apk/res-auto"
和
ads:adSize="BANNER"
。我在解决这个问题时记录了我的笔记本电脑屏幕。所以如果你喜欢视觉插图,你可以在 YouTube 上观看视频。
回答by FeelCode
Juste clean your app !
Juste 清理您的应用程序!
Build -> Clean Project
构建 -> 清理项目
The message dissapear.
消息消失。
回答by Nino Frenn
This happen when eclipse failed to syncronize your xml with google play library, try cleaning project and restart eclipse
当 eclipse 无法将您的 xml 与 google play 库同步时会发生这种情况,请尝试清理项目并重新启动 eclipse
回答by Libin
change as below ..remove ads:loadAdOnCreate="true"
更改如下 ..remove ads:loadAdOnCreate="true"
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="MyAdUnitId"/>
check this link, it has clear example
检查这个链接,它有明确的例子
https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play
https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play
回答by metomero
Add this in android manifest:
在 android manifest 中添加这个:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
That solved my problem, I hope it will solve yours.
这解决了我的问题,我希望它能解决你的问题。