2011-07-12 56 views
19

我真的不知道为什么,但我无法检索字符串的ID。Android getIdentifier不适用于字符串?

这是我的代码:

int resId = getApplicationContext().getResources() 
      .getIdentifier("About_EmailPrompt", "string", "com.yoki.android.cat"); 

但它完全适用于所有其他类R档的:

int resId = getApplicationContext().getResources() 
      .getIdentifier("arrow","drawable", "com.yoki.android.cat"); 

所以,我真的不明白是怎么回事,什么想法吗?

+2

你试过对项目执行“clean”吗?可能是一些旧的缓存搞乱了东西...... – Ognyan

+0

你是否检查'R.java'生成的文件中是否定义了'About_EmailPrompt'? –

回答

-3

我有这样的功能:

public static int getDrawable(Context context, String name) 
    { 
     Assert.assertNotNull(context); 
     Assert.assertNotNull(name); 

     return context.getResources().getIdentifier(name, 
       "drawable", context.getPackageName()); 
    } 

    public static int getString(Context context, String name) 
    { 
     Assert.assertNotNull(context); 
     Assert.assertNotNull(name); 

     return context.getResources().getIdentifier(name, 
       "strings", context.getPackageName()); 
    } 

我认为你应该使用字符串,而不是字符串

+4

不,“串”应该没问题。这对我来说很有用:'int id = getResources()。getIdentifier(key,“string”,getPackageName());' – Twilite

+1

请注意,标识符应该与我们引用它的方式相同。如果它是'R.string.some_string',那么第二个参数应该是'“string”',而不是''strings“'。 –

+0

它实际上必须是“字符串”而不是“字符串”。 – AlBirdie

4

我设法让我的应用程序工作的解决方案。因此,在原有基础上的问题,你需要去改变它,如下:

int resId = getApplicationContext().getResources() 
      .getIdentifier("com.yoki.android.cat:string/About_EmailPrompt", null, null); 

这是写则getIdentifier()方法的另一种方式,但它似乎以这种方式工作。

+0

getIdentifier需要3个参数。资源名称,资源类型和资源包。最后两个参数是可选的。如果最后两个没有提供,那么第一个参数必须包含完整的资源路径。这意味着下面的两个是相同的: 'INT渣油= getApplicationContext()getResources() .getIdentifier( “com.yoki.android.cat:string/About_EmailPrompt”,NULL,NULL);' 相同。 : 'int resId = getApplicationContext()。getResources() .getIdentifier(“About_EmailPrompt”,“string”,“com.yoki.android.cat”);' – user1959190

6

作为一个替代方法,你可以使用反射:

R.string.class.getField("string_name").getInt(null); 

按名称获取类的字段,然后让整数值基本上做的伎俩。

我们传递null作为对象,因为所有生成的资源字段都是静态的,因此不是对象的一部分。

如果您犯了拼写错误,您可能需要处理一些例外(NoSuchFieldException等),但它确实有效。

+0

有趣的方法,谢谢你的回答。 – Apqu

+0

它仍然显示像'#ff026cec'这样的值,而不是真正的字符串。 –

+0

是的,它是一个int,它是一个资源的id,因此被问到了什么。你需要使用getString来获取值。不管语言,id都是一样的,字符串不是。 – shalafi

0

您可以创建一个方法来替换所有的getIdentifier -calls。否则,你必须混合使用两种不同的方法,这会使代码有点不清楚。

此方法类似于@ shalafi的解决方案并使用反射。

public static int getResId(String variableName, Class<?> c) { 

    try { 
     Field idField = c.getDeclaredField(variableName); 
     return idField.getInt(idField); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return -1; 
    } 
} 

,并使用它像这样:

getResId("About_EmailPrompt", R.string.class); //or for other things 
getResId("icon", R.drawable.class); 


如果使用

public static int getResStringId(String variableName) { 
    try { 
     Field idField = R.string.getDeclaredField(variableName); 
     return idField.getInt(idField); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return -1; 
    } 
} 

是比getIdentifier()或上述溶液甚至更快。Source

13

此代码看起来正确的代码:

int resId = getApplicationContext().getResources().getIdentifier("About_EmailPrompt", "string", "com.yoki.android.cat"); 

,它应该工作,但如果没有,我劝你不要试图解决这个问题,并试图了解为什么它不工作:

1)首先试图找到在你的项目/项目未来declarated领域:

About_EmailPrompt 

如果你正在使用你文摘应该按按Ctrl + H和开放的Java搜索标签 Java Search Dialog

如果你搜索去结果第2步,否则转到步骤3

2)你应该得到的结果作为层次

<YOUR_PACKAGE_NAME> 
    -- R 
    -- string 
     -- About_EmailPrompt 

2.1)检查你在你的代码中写入正确的包名

.getIdentifier("About_EmailPrompt", "string", <YOUR_PACKAGE_NAME>); 

2.2)检查您在文字字符串中只使用拉丁字符:

"About_EmailPrompt" 

"string" 

2.3)检查你name属性在strings.xml

<string name="About_EmailPrompt">SOME_VALUE</string> 
只使用拉丁字符

3)如果您没有搜索结果

3.1)检查你使用你的文本字符串只是拉丁符号:

"About_EmailPrompt" 

3.2)检查你name属性在strings.xml

<string name="About_EmailPrompt">SOME_VALUE</string> 

3.3只使用拉丁字符)做清洁和构建对于您所有的项目

PS如果这对你没有帮助,尝试重新启动你的IDE(有时候eclipse生成R.java不正确,直到重新启动)

+0

感谢上帝,我终于找到了能够做到这一点的人。我现在可以哭了。谢谢 – glace

相关问题