简体   繁体   中英

Problems with Strings from String.xml and integers

I'm a beginner Android Developer My program compiles, but one line doesn't work properly.

Here is the line:

String m = R.string.mess2 + time/1000 + R.string.mess3;

The variable "time" is an int

And here is the Strings.xml file from where R.string.mess2 and R.string.mess3 are retrieved

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="mess2">Amazing, you read the End User License Agreement in </string>
   <string name="mess3"> seconds, go back and read it!</string>        
   </resources>

When the app is launched, where the strings should be takes place really long decimal.

This would usually work with As3 or C++, but it doesn't work here.

And one more thing, according to these responses, you make an array like this:

String[] MyString;

So the [ ] is placed in the variable type instead of the name?

You should be using the following code to get resources:

getResources().getString(R.string.someid)

Otherwise you're using the integer identifier of the resource, not the string from xml.

So in your case it should be

String m = getResources().getString(R.string.mess2) + time/1000 + getResources().getString(R.string.mess3);

R.string.mess2 is ResourceId.

You should be using

getString(R.string.mess2) to get string from string.xml in Android.

You might try

<string name="mess">Amazing, you read the End User License Agreement in %d seconds, go back and read it!</string>    

And java code will be

String m = String.format(context.getResources().getString(R.string.mess), time/1000);

You need to use :

YourActivity.this.getString(R.string.mess2)+time/1000 + YourActivity.this.getString(R.string.mess2);

Hope this helps.

在Android中使用getResources是一个好习惯,有时您可以忽略该警告,它会正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM