【发布时间】:2011-05-15 16:15:27
【问题描述】:
我刚刚花了几个小时处理一个可怕的与样式有关的 Android 资源预编译问题,我想知道任何人都可以解释是什么原因造成的。我修好了,但不知道是什么问题。 Android 说错误出现在 colors.xml 而不是另一个 xml 中并没有帮助。
第 1 步 - 重新创建错误
Given a style
<style name="ActionBarTitleContainer">
<item name="android:id">@id/title_container</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">@dimen/title_height</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@color/title_color_light</item>
</style>
我复制了它,给了它新的,android:id
<item name="android:id">@id/info_container</item>
<style name="ActionBarTitleContainer">
<item name="android:id">@id/title_container</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">@dimen/title_height</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@color/title_color_light</item>
</style>
<style name="ActionBarInfoContainer">
<item name="android:id">@id/info_container</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">@dimen/info_height1</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@color/title_color_light</item>
</style>
已保存并编译 id 中所说的错误
<style name="ActionBarInfoContainer">
<item name="android:id">@id/info_container</item>
Description Resource Path Location Type
error: Error: No resource found that matches the given name (at 'android:id' with value '@id/info_container'). styles.xml /MyApp/res/values line 68 Android AAPT Problem
所以我错误地在 id 前加了一个 +
<style name="ActionBarInfoContainer">
<item name="android:id">@+id/info_container</item>
然后出错
W/ResourceType( 2202): Bad resource table: header size 0xc or total size 0x4aba is not on an integer boundary
W/ResourceType( 2202): Bad resource table: header size 0xc or total size 0x4aba is not on an integer boundary
ERROR: Unable to parse generated resources, aborting.
'aapt' error. Pre Compiler Build aborted.
遗憾的是,我添加了很多样式等,而不仅仅是这个,所以花了很长时间回溯以找到错误。 Android 说在 colors.xml 中发生错误时没有帮助。
修复者
从 styles.xml 中删除 +
在 ids.xml 中放置新的 id
<style name="ActionBarTitleContainer">
<item name="android:id">@id/title_container</item>
....
</style>
<style name="ActionBarInfoContainer">
<item name="android:id">@id/info_container</item>
....
</style>
改变了 @+id/info_container 到 @id/info_container
并将 id 添加到 ids.xml
<resources>
<item type="id" name="title_container" />
<item type="id" name="info_container" />
知道这个错误是什么意思,因为我花了几个小时的痛苦才找到它。
Android aapt 编译器没有帮助,说错误是围绕 colors.xml 引起的。
这个错误是我编译 2.1U1 造成的吗?
我有最新的 SDK,但每个平台文件夹中都有一个 aapt,但日期是 2011 年 4 月。
并供将来参考,因为我发现关于 aapt 资源编译的信息很少。这里有一些提示给有资源编译错误的人,这样你就不会因为糟糕的谷歌错误报告而浪费时间:
提示 1 - 打开详细的 ANDROID 构建输出
Open Eclipse Preferences
Open in the list on the left
Android
Build
Set Build output to
Verbose
Hit OK
Open Console View
Window > View > Console
Do clean build or type a space in and file and hit save if you dont want to do full build
Resources compiler aapt will run first
and throw error
两个问题
问题 1 - 红色错误可能出现在令人困惑的位置的输出中
可能是由于在线程中抛出了异常,但在构建过程完全停止之前其他文件仍然可以编译。
请注意在最近的总线停止布局.xml 之后错误似乎是如何发生的。
[2011-05-15 16:22:25 - MyApp] (new resource id filechooser_file_view from /Users/user1/Documents/workspace/MyApp/res/layout/filechooser_file_view.xml)
[2011-05-15 16:22:25 - MyApp] (new resource id listbusmapsactivity_layout from /Users/user1/Documents/workspace/MyApp/res/layout/listbusmapsactivity_layout.xml)
[2011-05-15 16:22:25 - MyApp] (new resource id nearest_bus_maps_layout from /Users/user1/Documents/workspace/MyApp/res/layout/nearest_bus_maps_layout.xml)
[2011-05-15 16:22:25 - MyApp] (new resource id nearest_bus_stops_layout from /Users/user1/Documents/workspace/MyApp/res/layout/nearest_bus_stops_layout.xml)
[2011-05-15 16:22:25 - MyApp] W/ResourceType( 2247): Bad resource table: header size 0xc or total size 0x4aba is not on an integer boundary
[2011-05-15 16:22:25 - MyApp] W/ResourceType( 2247): Bad resource table: header size 0xc or total size 0x4aba is not on an integer boundary
[2011-05-15 16:22:25 - MyApp] ERROR: Unable to parse generated resources, aborting.
[2011-05-15 16:22:25 - MyApp] (new resource id open_web_page_activity from /Users/user1/Documents/workspace/MyApp/res/layout/open_web_page_activity.xml)
[2011-05-15 16:22:25 - MyApp] (new resource id send_email_activity from /Users/user1/Documents/workspace/MyApp/res/layout/send_email_activity.xml)
[2011-05-15 16:22:25 - MyApp] 'aapt' error. Pre Compiler Build aborted.
这不是错误所在的文件。
提示:如何找到导致问题的确切文件
查找错误消息之前提到的最后一个文件:
'aapt' error. Pre Compiler Build aborted.
这是
/send_email_activity.xml
问题:send_email_activity.xml 不是有错误的文件。
send_email_activity.xml 编译成功并被输出。
这是我们想要的 /send_email_activity.xml 之后的那个
问题:引发错误的文件名丢失。
如何在 /send_email_activity.xml 之后找到导致错误的文件
在控制台输出中向上滚动到
Files:
aapt 编译器列出了它找到并准备编译的所有文件。
幸运的是 Files: 中的顺序与下面抛出错误时的顺序相同
在 Files: 部分中查找要编译的最后一个文件 (send_email_activity.xml)
TIP In the Console window find the last file that compiled ok before the error
'send_email_activity.xml'
And do CTRL/CMD(Mac) + F for previous occurrences under the Files: section
我们可以在这里看到 /send_email_activity.xml 后面跟着 colors.xml
Files:
drawable/alphabet_bar_bg.xml
Src: /Users/clearbrian/Documents/workspace/LondonBusStopMaps/res/drawable/alphabet_bar_bg.xml
drawable/alphabet_separator_bg.xml
Src: /Users/clearbrian/Documents/workspace/LondonBusStopMaps/res/drawable/alphabet_separator_bg.xml
.......
layout/send_email_activity.xml
Src: /Users/clearbrian/Documents/workspace/LondonBusStopMaps/res/layout/send_email_activity.xml
values/colors.xml
Src: /Users/clearbrian/Documents/workspace/LondonBusStopMaps/res/values/colors.xml
.......
values/themes.xml
Src: /Users/clearbrian/Documents/workspace/LondonBusStopMaps/res/values/themes.xml
AndroidManifest.xml
Src: /Users/clearbrian/Documents/workspace/LondonBusStopMaps/AndroidManifest.xml
回答:colors.xml 是导致的文件 'aapt' 错误。预编译器构建已中止。
还有错误:
[2011-05-15 16:22:25 - MyApp] W/ResourceType( 2247): Bad resource table: header size 0xc or total size 0x4aba is not on an integer boundary
[2011-05-15 16:22:25 - MyApp] W/ResourceType( 2247): Bad resource table: header size 0xc or total size 0x4aba is not on an integer boundary
[2011-05-15 16:22:25 - MyApp] ERROR: Unable to parse generated resources, aborting.
问题 2:colors.xml 中的错误 ISNT。
我在colors.xml中改变了一个颜色值
但它已在styles.xml中被引用
styles.xml
<style name="ActionBarInfoContainer">
<item name="android:id">@id/info_container</item>
...
<item name="android:background">@color/title_color_light</item>
</style>
所以在编译过程中它可能会尝试编译styles.xml,然后看到@color,然后编译colors.xml。反之亦然(询问谷歌) 所以它在快速时将错误报告为在colors.xml中,它是styles.xml/ids.xml
只有当我看到 id 之间的差异时才注意到可能的错误
<style name="ActionBarTitleContainer">
<item name="android:id">@id/title_container</item>
...
</style>
<style name="ActionBarInfoContainer">
<item name="android:id">@+id/info_container</item>
...
</style>
线
<item name="android:id">@+id/info_container</item>
应该是(无+)
<item name="android:id">@id/info_container</item>
id应该在ids.xml中
<resources>
<item type="id" name="title_container" />
<item type="id" name="info_container" />
XML ..新千年的穿孔卡片!
【问题讨论】:
-
天才帖。非常感谢您提供的所有信息。这个问题我追了很久,终于可以解决了!
-
收到类似错误,但发生在我开始使用 eclipse 插件的重构字符串到资源功能添加字符串资源文件时。尝试为甚至没有被覆盖的 ActionbarSherlock 生成资源 ID 时,aapt 似乎失败了。
标签: android xml resources aapt