I am building an Android app, and I have a problem to get resource from string.xml.
I need to have a URL in String.xml
that will be used several times through the app.
I tried Resources.getText("my_url")
, but this does not work. Eclipse complains when I use this.
Do you think this开发者_JAVA技巧 is the best way to do ?
What you probably want is:
String myUrl = getString(R.string.my_url);
The getString()
method is in Context
which means that it's available directly in your Activity
class since Activity
is a subclass of Context
. (The getString()
method is also in Resources
but it's easier to call on it directly on your Activity
.)
What happens with your XML resources is that each is given a unique integer ID and this is added to the automatically generated R
class as a public static final int
. You then use these IDs to reference the resources. Have a look in the gen
folder in your Eclipse project and you'll find the R
class in there.
Do you ever refer this page: https://developer.android.com/guide/topics/resources/available-resources.html ?
If you want to retrieve the String represented by a resource ID, you can call the Context.getString()
method.
Or, you have to post Eclipse's complains.
精彩评论