cla*_*amp 16 xml resources android android-library
是否可以使用像应用程序项目中的库项目中定义的字符串之类的资源?如果是这样,怎么样?因为我似乎无法解决我想解决的字符串,如下所示:
String title = "sample";
int id = ressources.getIdentifier(title, "string", "com.package");
Run Code Online (Sandbox Code Playgroud)
给了我这个例外
WARN/ResourceType(278):获取资源号0x00000000的值时没有包标识符WARN/System.err(278):android.content.res.Resources $ NotFoundException:字符串资源ID#0x0
我正在寻找的字符串("sample")肯定是在这个包中提供的库项目的strings.xml中.我甚至可以在R.java中看到它
Ber*_*t F 14
所以它看起来像
int id = ressources.getIdentifier(title,"string","com.package");
正在返回0,这意味着它无法找到指定的资源.后续调用ressources.getIdentifer()导致异常,因为0它不是有效的资源ID.
以下是一些调试/替代想法:
你可能已经做了十几次了,但这并没有伤害提到:首先重新检查所有内容的拼写:
uses-library元素中的库拼写正确的AndroidManifest.xml,title)还是特定于该类型的资源(字符串)?你能访问另一个图书馆的资源吗?Android - 是否可以创建一个自定义库以在多个应用程序中使用?
码
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
}
Run Code Online (Sandbox Code Playgroud)
码
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
}
Run Code Online (Sandbox Code Playgroud)
http://daniel-codes.blogspot.com/2009/12/dynamically-retrieving-resources-in.html
| 归档时间: |
|
| 查看次数: |
13270 次 |
| 最近记录: |