I really don't know why but I can't retrieve the Id of a String.
This is my code:
int resId = getApplicationContext().getResources() .getIdentifier("About_EmailPrompt", "string", "com.yoki.android.cat");
But it works perfectly for all other classes of R file:
int resId = getApplicationContext().getResources() .getIdentifier("arrow","drawable", "com.yoki.android.cat");
So I really don't understand what's going on here, any ideas ?
This code looks like correct code:
int resId = getApplicationContext().getResources().getIdentifier("About_EmailPrompt", "string", "com.yoki.android.cat");
and it should work, but if not I advise you do not try to get around this and try to understand Why it doesn't work:
1) First of all try to find next declarated field in your project/projects:
About_EmailPrompt
if you are using eclips you should press Ctrl+H and open Java Search tab
If you get results for searching go to step 2 otherwise go to step 3
2) You should get result as hierarchy
<YOUR_PACKAGE_NAME> -- R -- string -- About_EmailPrompt
2.1) check you write correct package name in your code
.getIdentifier("About_EmailPrompt", "string", <YOUR_PACKAGE_NAME>);
2.2) check you use just latin symbols in your literal strings:
"About_EmailPrompt"
and
"string"
2.3) check you use just latin symbols in name
attribute in strings.xml
<string name="About_EmailPrompt">SOME_VALUE</string>
3) If you have no search result
3.1) check you use just latin symbols in your literal string:
"About_EmailPrompt"
3.2) check you use just latin symbols in name
attribute in strings.xml
<string name="About_EmailPrompt">SOME_VALUE</string>
3.3) Do clean and build for all projects you have
P.S. If this does not help you, try to restart your IDE (sometimes eclipse generate R.java incorrectly until restarting)
As an alternative approach, you can use reflection:
R.string.class.getField("string_name").getInt(null);
Which essentially does the trick by getting a field of the class by name and then getting the integer value.
We pass null as the object because all the resources generated field are static and therefore not part of an object.
You may need to handle a few exceptions (NoSuchFieldException, etc) in case you made a typo, but it does work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With