FET
FET

Reputation: 942

R.string not working onCreate()

I need to display a 'dynamic text' which is concatenated by 2 R.strings and 1 String variable:

The fact is I need to display it at the beginning so on the onCreate() method, but I get this error:

Caused by: android.content.res.Resources$NotFoundException: String resource ID #

This is what I tried to do:

text2.setText(R.string.still + playersAmount + R.string.LastRemaining); //Display Single

Any ideas?

Upvotes: 0

Views: 1193

Answers (4)

David Medenjak
David Medenjak

Reputation: 34532

If I tell you to call first one phone number, then the other, you can't just sum them up and call the result. This is what you're doing.

R.string.some_id is an integer that maps to a string and can be localized. The localization works by mapping the same id to different values for different languages. It can only be resolved though, if you look it up using its id, and only that. Modifying that id will lead to invalid results: This is the error you see.

You need to get those 2 Strings seperately.

getString(R.string.first) + playersAmount + getString(R.string.second)

Then the normal rules for string concatenation apply.

Also, you should have a look on string formatters since they reduce the need for manual concatenation, which will lead to problems if you ever add multiple languages.

<string name="formatted_string">I have %d players</string>

Can also be used like this:

context.getString(R.string.formatted_string, playersAmount);

Upvotes: 2

Gut
Gut

Reputation: 331

When you do R.string.still + playersAmount + R.string.LastRemaining you are juste adding 3 int together (and looking for a resource string at the result)

If you wanna get a String from resources you must use getString(int)

http://developer.android.com/reference/android/content/Context.html#getString(int)

for you solution text2.setText(getString(R.string.still) + playersAmount + getString(R.string.LastRemaining));

I suggest you use a string with parameters <string name="stillLastRemaining">Still %d last remaining</string> and use text2.setText(getString(R.string.stillLastRemaining, playersAmount));

Upvotes: 0

technoplato
technoplato

Reputation: 3421

A more complete explanation of the solution to your problem (that builds on inner_class7's explanation) is that you'll need to do the following:

// Get Strings from resources
String still = getResources().getString(R.sting.still);
String lastRemaining = getResources.getString(R.string.LastRemaining);

// Concatenate Strings and set on TextView
text2.setText(still + playersAmount + lastRemaining);

As he mentioned, when you say R.string.still, you're really talking about an integer reference, which isn't what you want to do here. The magic that happens here is that getString is "knows" that you want the String associated with that particular ID. Let me know if you have any questions.

Upvotes: 0

kandroidj
kandroidj

Reputation: 13932

call getResources().getString(R.string.still)

and getResources().getString(R.string.LastRemaining)

otherwise you are just referencing an integer value from R.java adding it to playersAmount and then adding another integer value from R.java and Android is looking for this sum of ints as the Strings resource id.

Upvotes: 1

Related Questions