简体   繁体   English

无法在android库项目中添加id资源类型文件

[英]Cannot add id resource type file in android library project

In my android application I have created 2 projects. 在我的Android应用程序中,我创建了2个项目。 The main project and a library one since it will be used in future projects as well. 主要项目和图书馆,因为它将在未来的项目中使用。 Since I would like to reference some views in code I tried to create a resource type file to declare some unique ids as stated here http://developer.android.com/guide/topics/resources/more-resources.html#Id . 由于我想在代码中引用一些视图,我尝试创建一个资源类型文件来声明一些独特的ID,如http://developer.android.com/guide/topics/resources/more-resources.html#Id中所述

The problem is that when I add this file to res/values (called ids.xml) the R file is not generated anymore. 问题是,当我将此文件添加到res / values(称为ids.xml)时,不再生成R文件。 When I delete it everything works fine. 当我删除它一切正常。

I know that I can add an id resource type file in an android project. 我知道我可以在android项目中添加id资源类型文件。 Is there a limitation to do the same in a library project? 在图书馆项目中做同样的事情有限制吗?

Thank you, Bill 比尔谢谢你

After 2 days of research I finally found the solution. 经过2天的研究,我终于找到了解决方案。

The post [here][1] 帖子[这里] [1]

[1]: Android style Resources compile (aapt) failing : Bad resource table: header size 0xc helped me. [1]: Android风格资源编译(aapt)失败:错误的资源表:标题大小0xc帮助了我。

It doesn't matter if it's an Android project or a library one, but when you have declared a new id in one of your styles then you cannot add an ids.xml file to declare unique ids. 无论是Android项目还是库项目都无关紧要,但是当您在其中一个样式中声明了新ID时,则无法添加ids.xml文件来声明唯一ID。

In my case, I had declared a style in my res/values/styles.xml 在我的例子中,我在res / values / styles.xml中声明了一个样式

<style name="ActionBar">
        <item name="android:id">**@+id/actionbar_container**</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">@dimen/actionbar_height</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">@drawable/actionbar_gradient</item>
    </style>

and then added a res/values/ids.xml where I declared some unique ids. 然后添加了res / values / ids.xml,其中我声明了一些唯一的ID。 For example, 例如,

<resources>
<item type="id" name="menu_refresh" />
</resources>

After adding this file, R stopped generating. 添加此文件后,R停止生成。 Project Clean didn't do the work. Project Clean没有做好工作。

When I changed the declaration in styles.xml to @id/actionbar_container, R started generating again. 当我将styles.xml中的声明更改为@ id / actionbar_container时,R再次开始生成。 The difference is that I have declared the id in my ids.xml file while I reference in the style above using @id and not @+id. 区别在于我在我的ids.xml文件中声明了id,而我使用@id而不是 @ + id引用了上面的样式。

Conclusion: If you want to declare unique ids in a resource file (ie ids.xml) double-check that you have not declared any new ids (using the @+id syntax) in your styles first. 结论:如果要在资源文件(即ids.xml)中声明唯一ID,请首先仔细检查您的样式中是否未声明任何新ID(使用@ + id语法)。

Hope that helps anyone with the same problem. 希望能帮助任何有同样问题的人。

Happy coding!!! 快乐的编码!

Just in case, as it happened to me... I spent minutes trying to understand why my "R.id.myid" could not be found in java file after creating it as a resource id item: 为了以防万一,因为它发生在我身上......我花了几分钟试图理解为什么我的“R.id.myid”在创建为资源id项后无法在java文件中找到:

<resources>
<item type="id" name="myid" />
</resources>

=> Problem was that my java file was importing "android.R" instead of my own "R" (generated resources file). =>问题是我的java文件导入了“android.R”而不是我自己的“R”(生成的资源文件)。

import android.R;

Sounds stupid but it can happen. 听起来很愚蠢,但它可能会发生。

import xxxx.xxxx.R;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM