views:

207

answers:

1

I thought I'd be classy and use the string.xml file to define some constant strings for things like exception messages. In strings.xml I hit Add, chose the "String" option (not 'String Array'), then gave it a name and value. I was surprised to see that this code doesn't work:

throw new Exception(R.string.MyExceptionMessage);

And that fails because R.string.MyExceptionMessage is actually of type int. I can verify that type by looking in R.java. What am I missing?

+5  A: 
Christopher
Chris is right on both accounts. Context.getString loads your string resource. But beware of putting non-localizable stuff there as it will mix up with your strings for translation. I wouldn't consider it classy, but a MUST to put your UI strings in the XML files and not in the code. At a certain point you will want to distribute your apps to other countries, and to get them out of the code will require tons of effort. Do it now and save the headache. I have a distributor in China I just send her the strings.xml for translation, no effort. Regards, Ari
BeMeCollective