开发者

How to properly reference the Strings.xml file, android

My string.xml has a string:

<string name="loginLocation">http://website.com/afile.php</string>

In my java file, I am trying to reference it like so:

url = new URL(R.string.loginLocation);

except I am getting the error:

The constructor URL(int) is undefined

I managed to get the error to go away by doing:

url = new URL(Integer.toString(R.string.loginLocation));

except when I make the call to it, I get a Protocol Error

I can do:

url = new URL("http://w开发者_运维技巧ebsite.com/afile.php");

and it works fine, but I'd like to define it in the Strings.xml file. Any help is appreciated, thanks!


If you're trying to do it in a method of the Activity subclass, then do the following:

url = new URL(getString(R.string.loginLocation));


getResources() will give you a lot of methods for accessing what's in your /res directory. Among the rest, the method you are looking for is getString(R.string....), which is also available directly from your Context (without the Resources object).


As simple as this:

this.getxt(R.string.btn_menu_logout);

What nobody mentions online is to put .this before gettxt() or getText():

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜