【问题标题】:error: No resource identifier found for attribute 'adSize' in package 'com.google.example' main.xml错误:在包“com.google.example”main.xml 中找不到属性“adSize”的资源标识符
【发布时间】:2017-05-26 11:21:03
【问题描述】:

当我按照说明通过 xml 将广告添加到我的应用程序时,我收到以下错误:

Description Resource Path Location Type
error: No resource identifier found for attribute 'adSize' in package 'com.google.example'  main.xml    /HelloWorld/res/layout  line 12 Android AAPT Problem
Description Resource Path Location Type
error: No resource identifier found for attribute 'adUnitId' in package 'com.google.example'    main.xml    /HelloWorld/res/layout  line 12 Android AAPT Problem

我确实编辑了main.xml,添加了attrs.xml 文件,但编译器不喜欢它。

【问题讨论】:

  • 粘贴布局和 attrs.xml

标签: android admob


【解决方案1】:

在您的自定义布局命名空间中将 /res/ 替换为 /lib/

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

在你的情况下,将是:

xmlns:yourApp="http://schemas.android.com/apk/lib/com.yourAppPackege.yourClass"

【讨论】:

  • 我遇到了不同的问题,但同样的解决方案有效。基本上,我使用的布局文件引用了 /res 文件,我按照此处的说明将其更改为 /lib 并且它有效。你能解释一下这里发生了什么吗?
  • xmlns:yourApp="schemas.android.com/apk/res-auto";
  • 嗨,以上解决方案中的 yourApp 和 yourClass 是什么?
  • 请写一个例子
  • 每当我这样做并重新构建项目时它会自动将其转换为以前的项目并引发错误
【解决方案2】:

我遇到了同样的问题。我从 Google 代码中复制了示例代码,但无法编译。

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

最后,我想通了。代码“com.google.example”的最后一部分是他们的包名,所以你需要用你的项目包替换它。

比如我的项目包是“com.jms.AdmobExample”,那么我的ads命名空间是:

xmlns:ads="http://schemas.android.com/apk/res/com.jms.AdmobExample"

检查我的示例,它工作正常。你可以下载APK试试。我也把我的源代码放在这里:Add Google Admob in Android Application

【讨论】:

  • Devs,如果您在 xmlns:ads 中使用您的项目包,那么您还需要按照 James 在其链接中的说明添加“attrs.xml”。对于较新的 Admob sdk,不再推荐使用 attrs.xml。只需使用以下命名空间:xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  • @vivek.m,谢谢你给我指出正确的答案!相反 apk/res 必须使用 apk/lib 以防“apklib”项目依赖。
  • 谢谢,我喜欢互联网,特别是 Stackoverflow.com
  • 非常感谢,经过 3 小时的搜索和调试,您的答案终于弹出并修复了。
  • @vivek.m 谢谢,这是对我有用的答案——使用 com.google.ads。
【解决方案3】:

对我来说,我必须添加

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

紧接着:

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

在 res/layout/main.xml 中

【讨论】:

【解决方案4】:

您也可以使用http://schemas.android.com/apk/res-auto 来自动处理它。像这样使用它:

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

【讨论】:

  • @Niklas:这不是网页链接。这是一个要像这样使用的 URL:xmlns:ads="schemas.android.com/apk/res-auto",它会自动解析资源 URL。请参阅上面的更新答案。
【解决方案5】:

我遇到了同样的问题,但是在使用库项目时。该问题已在 r17 中解决: 而不是使用包的命名空间:

xmlns:app="http://schemas.android.com/apk/res/hu.droidium.exercises"

必须使用虚拟命名空间:

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

这将解决无法从引用项目访问属性的问题。

【讨论】:

    【解决方案6】:

    我从Android.com 下载了相同的自定义演示并遇到了相同的编译问题。

    一开始,我就改变了

    xmlns:custom="http://schemas.android.com/apk/res/com.example.android.customviews"
    

    xmlns:custom="http://schemas.android.com/apk/lib/com.example.android.customviews"
    

    它工作。然后我得到另一个解决方案

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

    它也有效,但有一些不同之处。第二种解决方案具有完善的功能。我正在寻找原因,也许你可以帮忙。谢谢

    【讨论】:

    • "schemas.android.com/apk/res-auto" 添加您项目中定义的所有属性以及您在项目中引用的库中定义的所有属性。因此它适用于您及其更清洁的方法
    【解决方案7】:

    正如您在 attrs.xml 中指定的那样,您的 adSize 属性属于命名空间 com.google.ads.AdView。尝试改变:

    android:adUnitId="a14bd6d2c63e055"         android:adSize="BANNER"
    

    ads:adUnitId="a14bd6d2c63e055"         ads:adSize="BANNER"
    

    它应该可以工作。

    【讨论】:

    • @FranciscoCorralesMorales 我确实添加了它,但它仍然无法正常工作
    【解决方案8】:

    我收到了关于 largeHeap 属性的错误,我的应用程序没有在 eclipse 下运行,但在 ant 下它仍然可以正常构建和运行。

    android documentation 声明:

    属性:

    xmlns:android

    定义 Android 命名空间。此属性应始终设置为“http://schemas.android.com/apk/res/android”。

    我在清单中删除了该行,保存在 eclipse 中,将该行粘贴回并再次保存,它起作用了。在我的情况下,我猜问题是 eclipse、ant 和 adb 没有正确地相互交谈并且保存重置了一些东西。有趣的是重启 eclipse 并没有解决这个问题(通常这些类型的问题重启 eclipse 是你应该尝试的第一件事,通常它可以解决问题)。

    【讨论】:

    【解决方案9】:

    确保您已将此部分包含在布局中(顶部位于 xmlns:android 行下方)

    xmlns:ads="http://schemas.android.com/apk/res/com.google.example" 
    ...........blah blah..
    

    还要检查是否在 res/values/ 中包含 attrs.xml

    点击此处了解更多详情。 http://code.google.com/mobile/ads/docs/android/banner_xml.html

    【讨论】:

      【解决方案10】:

      我通常以这种方式将 xmlns:ads 属性嵌入到 adview 属性中:

      <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"
          android:layout_alignParentBottom="true"
          ads:adSize="BANNER"
          ads:adUnitId="@string/banner_ad_unit_id"/>
      

      这样您就不需要每次复制广告视图时都嵌入到父级中。

      只需复制并粘贴上面的广告视图并将其粘贴到任何地方,它应该可以工作

      【讨论】:

        【解决方案11】:

        我一直在寻找答案,但找不到,但最后我可以通过添加 play-service-ads 依赖来解决这个问题,让我们试试这个:

        *) 文件 -> 项目结构... -> 在模块下你可以找到 app 并且有一个名为 dependencies 的选项,你可以添加 com.google.android.gms:play-services-ads: xxx 对你的项目的依赖

        我在尝试将 Eclipse 项目导入 Android Studio 时遇到了这个问题。

        【讨论】:

          【解决方案12】:

          就我而言,我在应用级别 build.gradle 中缺少一个依赖项。

          解决方案是添加依赖项,如下谷歌概述:

          为您的应用打开应用级 build.gradle 文件,并查找“依赖项”部分。

          dependencies {
              implementation ...SOME_IMPLEMENTATION
              implementation ...SOME_IMPLEMENTATION
              implementation 'com.google.android.gms:play-services-ads:18.1.1'//<---ADD THIS LINE
          }
          

          添加上面的行,指示 Gradle 拉入最新版本的移动广告 SDK 和其他相关依赖项。完成后,保存文件并执行 Gradle 同步。

          【讨论】:

            【解决方案13】:

            基于答案here,我认为您需要更改 xmlns:ads 属性。例如,改变这个:

            <com.google.ads.AdView 
                xmlns:ads="http://schemas.android.com/apk/res/com.google.example"
                ...
                />
            

            到这里:

            <com.google.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res/com.your.app.namespace" 
                ... 
                />
            

            它为我修复了它。如果您仍然遇到错误,能否详细说明?

            【讨论】:

              【解决方案14】:

              我也遇到了同样的问题,我使用GoogleAdMobAdsSDK-4.1.0.jar 然后我尝试使用GoogleAdMobAdsSDK-4.0.4.jar 现在它工作正常,根据我的经验,这是 jar 文件的问题。

              【讨论】:

                【解决方案15】:

                在使用 xml 中具有 "android:" 属性的 Drawables 和 Layouts 文件构建类库时,我在 MonoDroid 上遇到了类似的问题。我收到与问题中的错误类似的错误。

                在中未找到属性“textCursorDrawable”的资源标识符 包'android'

                我发现“android:”属性仅在 Android API Level 12+ 中可用,我正在尝试为旧版本构建。更新我的项目以针对 Android 4.0 构建解决了我的问题。 这是我找到答案的地方。 https://groups.google.com/forum/?fromgroups#!topic/android-developers/ocxKphM5MWM 如果您遇到类似问题,请确保您正在针对正确的 API 级别进行构建,并确保您构建所针对的 API 级别中存在缺少的标识符。

                【讨论】:

                  【解决方案16】:

                  只需更改目标 sdk 右键单击​​项目然后单击 属性选择android并选择最新的API

                  【讨论】:

                    【解决方案17】:

                    res/layout 文件夹中的“activity_banner_xml.xml”文件中发生了同样的错误。我所做的修复它是替换

                    <com.google.android.gms.samples.ads.AdView
                    

                    <com.google.android.gms.ads.AdView
                    

                    没有对我的包名的引用。 另外,请确保将 adUnitId 设置为 ads:adUnitId="@string/banner_ad_unit_id"&gt; 添加单元 ID 位于您的 res/Values/Strings 文件夹中。

                    所以我的最终布局文件如下所示:

                    <?xml version="1.0" encoding="utf-8"?>
                    <RelativeLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:ads="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/mainLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <com.google.android.gms.ads.AdView
                        android:id="@+id/adView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        ads:adSize="BANNER"
                        ads:adUnitId="@string/ad_unit_id"/>
                    

                    【讨论】:

                      【解决方案18】:

                      如果您的项目是 gradle 项目,请替换:

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

                      与:

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

                      【讨论】:

                        【解决方案19】:

                        我正在添加适用于我的相同错误的解决方案。

                        我右键单击项目>属性>(左面板)Android

                        Library下的右侧面板我删除了有问题的库并重新添加。

                        对我来说,这个错误发生在一个损坏的工作区 eclipse 文件之后,我不得不再次导入所有项目。

                        【讨论】:

                          【解决方案20】:

                          我的问题非常相似(产生同样的问题)。在通过 Android Studio 中的“refactor -> rename”选项重构变量名(从“value”到“myValue”)后,我也发现了清单文件中的更改。元数据“价值”池已更改为“myValue”。

                          <meta-data
                                  android:name="com.facebook.sdk.ApplicationId"
                                  android:myValue="@string/facebook_app_id"/>
                          

                          还原文件后,一切都恢复正常了。

                          <meta-data
                                  android:name="com.facebook.sdk.ApplicationId"
                                  android:value="@string/facebook_app_id"/>
                          

                          希望对大家有所帮助!

                          【讨论】:

                            【解决方案21】:

                            添加xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads", 这将解决您的问题。

                            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                xmlns:tools="http://schemas.android.com/tools"
                                xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                                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="com.app.activities.Initializer" >
                            
                            
                                <com.google.android.gms.ads.AdView
                                    android:id="@+id/adView"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    ads:adSize="BANNER"
                                    ads:adUnitId="ca-app-pub-123456789/123456789"/>
                            
                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="@string/hello_world" />
                            
                            </RelativeLayout>
                            

                            【讨论】:

                              【解决方案22】:

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

                              然后重建项目

                              【讨论】:

                                【解决方案23】:

                                我在android.support.design.widget.NawigationView中添加了这个参数:

                                android:layout_gravity="start"
                                

                                问题就解决了。

                                【讨论】:

                                  【解决方案24】:

                                  我遇到了同样的问题,我的解决方法是简单地在我的应用模块的 build.gradle 中启用数据绑定。

                                  机器人{ ... dataBinding.enabled = true }

                                  参考-https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/

                                  【讨论】:

                                    【解决方案25】:

                                    compile 'de.hdodenhof:circleimageview:2.0.0' IN Gradle Dependencies 并将这段代码放在 nav_header_main.xml 中

                                    <?xml version="1.0" encoding="utf-8"?>
                                    <LinearLayout
                                        xmlns:android="http://schemas.android.com/apk/res/android"
                                        xmlns:app="http://schemas.android.com/apk/lib/org.mainsoft.navigationdrawer"
                                    
                                        android:gravity="bottom"
                                        android:paddingBottom="@dimen/activity_vertical_margin"
                                        android:paddingLeft="@dimen/activity_horizontal_margin"
                                        android:paddingRight="@dimen/activity_horizontal_margin"
                                        android:paddingTop="@dimen/activity_vertical_margin"
                                        android:theme="@style/ThemeOverlay.AppCompat.Dark"
                                        android:layout_width="match_parent"
                                        android:layout_height="160dp"
                                        android:background="@drawable/background_material_red"
                                        android:orientation="vertical">
                                    
                                    <de.hdodenhof.circleimageview.CircleImageView
                                    
                                        android:id="@+id/profile_image"
                                        android:layout_width="70dp"
                                        android:layout_height="70dp"
                                        android:src="@drawable/profile"
                                        app:border_color="#FF000000"
                                        android:layout_marginLeft="24dp"
                                        android:layout_centerVertical="true"
                                        android:layout_alignParentLeft="true"
                                        android:layout_alignParentStart="true"
                                        android:layout_marginStart="24dp" />
                                    
                                    </LinearLayout>
                                    

                                    【讨论】:

                                      猜你喜欢
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2012-08-02
                                      • 1970-01-01
                                      • 2015-04-11
                                      • 2013-12-09
                                      • 1970-01-01
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多