14

我刚刚花了几个小时处理一个与样式有关的可怕的 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 - 打开 VERBOSE 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 - 红色错误可能出现在混乱位置的输出中

可能是由于在线程中抛出异常引起的,但在构建过程完全停止之前其他文件仍然可以编译。

请注意在最近的_bus_stops_layout.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 中被引用

样式.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 ..新千年的穿孔卡片!

4

2 回答 2

7

我在导入库项目(GreenDroid)时遇到了这个问题,结果对我来说也是@+id 问题。

似乎正在发生的事情是,只要您没有ids.xml文件,Android SDK 就会允许您在styles.xml中声明新的 id ,但是一旦该文件存在,它就会停止允许这种行为。不幸的是,它并没有告诉你这是问题所在,只是因为原始帖子中提到的错误而崩溃:

W/ResourceType(2247):错误的资源表:标头大小 0xc 或总大小 0x4aba 不在整数边界上
于 2012-01-19T20:08:22.013 回答
1

此页面上,您将找到有关+. 页面文档布局 XML 文件,但同样适用于样式(是的,他们应该将此信息放在更好的位置)。

为方便起见,以下是相关文档:

对于 ID 值,您通常应该使用以下语法形式:“@+id/name”。加号 + 表示这是一个新的资源 ID,如果它不存在,aapt 工具将在 R.java 类中创建一个新的资源整数。

...

但是,如果您已经定义了一个 ID 资源(并且尚未使用),那么您可以通过排除 android:id 值中的加号将该 ID 应用于 View 元素。

于 2011-11-02T16:06:54.303 回答