Android中的AdMob "AdView缺少必需的XML属性'adSize'"

4
我正在尝试在我的应用程序中实现AdMob。但不知怎么地,它显示出这个错误,并且由于它,我的R.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/lib/com.google.ads" 
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:background="@color/bgcolor">

<LinearLayout
android:id="@+id/Linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adSize="BANNER"
                         ads:adUnitId="XXX"
                         ads:refreshInterval="60"/>


</LinearLayout>

请帮我一下,我被困住了 :(

3个回答

17

可能是命名空间问题。您需要定义命名空间。

尝试添加

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


我尝试添加了上面的这行,但还是不起作用。为了让上述内容起作用,我是否还需要在“res”目录中创建'lib'文件夹并将AdMob jar复制到其中? - YuDroid
后来当我再次重建和清理项目时,上述解决方案对我起作用了。感谢Eren。 - YuDroid
这里还有一件事情需要做的是,当我尝试使用“libs”文件夹来运行上述代码时,广告没有显示出来。但是当我将其更改为“lib”时,它可以正常工作,没有任何问题。 - YuDroid

6
新版Admob SDK(Google Play服务)需要另一个命名空间。
xmlns:ads="http://schemas.android.com/apk/res-auto"

3
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent" android:id="@+id/rltvLayoutPromote"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayoutwebview"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:orientation="vertical">
        <WebView android:id="@+id/webViewPromote"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:fitsSystemWindows="true" />
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/ad_layout" android:layout_height="wrap_content"
        android:gravity="bottom" android:layout_alignParentBottom="true"
        android:layout_alignBottom="@+id/home_layout">
        <com.google.ads.AdView android:layout_width="wrap_content"
            android:layout_height="wrap_content" ads:adUnitId="XXXXXXXXXX"
            ads:adSize="BANNER" android:id="@+id/adView" ads:refreshInterval="60" />
        <!--            put 3 if not working-->
    </LinearLayout>
</RelativeLayout>

将这些行放入manifest.xml文件中

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />

对我来说,上述代码完美运行...访问此网站以获取完整参考资料help forandroid-admob
谢谢Pragna


我想做的是以下小片段。你能检查一下并告诉我哪里出错了吗? - YuDroid
由于我是初学者,我不能添加超过允许字符限制的注释。因此,我已经编辑了我的问题并在其中包含了更多的代码。 - YuDroid
你正在使用哪个APK版本?请告诉我。 - Android
是的,你的代码对我有效,我已经尝试使用Android 1.4。我认为你可能忘记添加JAR文件,请访问此网站http://code.google.com/mobile/ads/docs/android/fundamentals.html获取完整参考资料。 - Android
你是否已经放置了你的广告ID ads:adUnitId="HERE"? - Android
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接