Resources.getIdentifier()生成资源ID

时间:2014-05-09 18:04:15

标签: android android-resources

如果我正确理解文档,则Resources.getIdentifier()的调用将返回0,以防name的资源不存在。 API级别为8的仿真器以这种方式工作。

但是,如果我使用API​​级别为16的模拟器,它会为我创建一个新的ID。我甚至可以在其上调用Resources.getResourceName()来获取我在Resources.getIdentifier()中使用的名称。如果我打电话给Resources.getString(),它会抛出异常。

int resourceId;
// both calls return the same value:
resourceId = resources.getIdentifier("com.testthisout:string/idontexist", null, null);
resourceId = resources.getIdentifier("idontexist", "string", "com.testthisout");
// with API-level 8 resourceId is 0.
// with API-level 16 it's something like 0x7f07005f now.
resources.getResourceName(resourceId);     // returns "com.testthisout:string/idontexist"
resources.getString(resourceId);           // BANG!!!

android.content.res.Resources$NotFoundException: String resource ID #0x7f07005f
    at android.content.res.Resources.getText(Resources.java:229)
    at android.content.res.Resources.getString(Resources.java:313)
    at android.content.res.Resources.getString(Resources.java:341)
    <my method here, no nested exception or something>

Resources.getIdentifier()使用不存在的资源名称返回所有进一步调用的递增数字,显然它将它们存储在某处。

我已经从头开始创建了一个新的模拟器,但仍然具有相同的行为。我在一个不同的项目中使用robolectric进行了测试(就像他们的指南解释的那样链接到我的主项目中),但如果我关闭该项目(我认为他们的阴影机制以某种方式导致问题)它没有帮助。清理和重建项目没有帮助,没有警告或错误。

我需要这个功能,因为我正在使用通用资源名称和后台更多逻辑。

预计会出现这种情况吗?有什么东西我做错了吗?

0 个答案:

没有答案
相关问题