从库项目的android进口ressources [英] android import ressources from library project
问题描述
是有可能使用ressources一样,在库项目中的应用程序项目定义字符串?如果是这样,怎么样? 因为我似乎无法解析字符串我想解决这样的:
is it possible to use ressources like strings that are defined in library projects in the application-projects? if so, how? because i cant seem to resolve the strings i would like to resolve like this:
String title = "sample";
int id = ressources.getIdentifier(title, "string", "com.package");
给我这个异常
gives me this exception
WARN /的ResourceType(278):没有包装标识的资源数量00000000获得价值时, WARN / System.err的(278):android.content.res.Resources $ NotFoundException:字符串资源编号为0x0
WARN/ResourceType(278): No package identifier when getting value for resource number 0x00000000 WARN/System.err(278): android.content.res.Resources$NotFoundException: String resource ID #0x0
的字符串(样本),我期待的无疑是在库项目的strings.xml中提供了这个包。我甚至可以看到它在R.java
the string ("sample") i am looking for is definitely in this package provided in the strings.xml of the library project. i can even see it in the R.java
推荐答案
因此,它看起来像
INT ID = ressources.getIdentifier(标题,串,com.package);
int id = ressources.getIdentifier(title, "string", "com.package");
在返回 0
,这意味着它无法找到指定的资源。随后调用 ressources.getIdentifer()
引起的异常,因为 0
不是一个有效的资源ID。
is returning 0
, meaning it can't find the specified resource. The subsequent call to ressources.getIdentifer()
causes the exception, since 0
is not a valid resource id.
下面是一些调试/另类的想法:
Here are some debug/alternative ideas:
-
您可能已经这样做了十几次,但它不会伤害提:第一复检一切拼写:
You've probably already done this a dozen times, but it doesn't hurt mentioning: first recheck spelling of everything:
- 在包的拼写是否正确(无论是在图书馆项目和客户项目),
- 在资源字符串是正确的(库项目和客户端项目),
- 是图书馆的拼写在
正确的用途库
元素中的的AndroidManifest.xml
, - 等
- package spelling is correct (both in library project and in client project),
- resource string is correct (library project and client project),
- is library spelling correct in the
uses-library
element theAndroidManifest.xml
, - etc.
- 您可以访问任何资源在库或问题特定于该资源(
标题
)或者是特定于该类型的资源(字符串)的?您可以访问其他库的资源?
- Can you access any resources in that library or is the problem specific to that resource (
title
) or is it specific to that type of resource(strings)? Can you access the resources of another library?
- 您访问的库作为一个jar文件?您可以罐子code,但你不能从一个罐子访问资源。
<一个href="http://stackoverflow.com/questions/1828448/android-is-it-possible-to-create-a-custom-library-to-use-across-several-appilca/1830774#1830774">Android - 是否有可能创建一个自定义库在多个appilcations使用
- 在你尝试备用名称格式为:
code
String fullyQualifiedResourceName = "com.package:string/sample";
int id = ressources.getIdentifier(title, null, null);
if (id == 0) {
Log.e(TAG, "Lookup id for resource '"+fullyQualifiedResourceName+"' failed";
// graceful error handling code here
}
- 您可以尝试使用反射:
- You could try using reflection:
code
final String resourceName= "sample";
final int id;
try {
final Class resourceType = com.package.R.string.class;
final Field field = resourceType.getField(title);
id = field.getInt(null);
} catch (final Exception e) {
Log.e(TAG, "Lookup id for resource '"+resourceName+"' failed";
// graceful error handling code here
}
<一个href="http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string/4428288#4428288">Android,从字符串获取资源ID?
<一个href="http://daniel-$c$cs.blogspot.com/2009/12/dynamically-retrieving-resources-in.html">http://daniel-$c$cs.blogspot.com/2009/12/dynamically-retrieving-resources-in.html
这篇关于从库项目的android进口ressources的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!