JFreeman
JFreeman

Reputation: 1004

android.content.res.Resources$NotFoundException when string resource exists

I have a few strings which have been put in the android values/strings.xml file in my android project.

<resources>
    <string name="level_reached_label">Level Reached: </string>
    <string name="endgame_textP1">You have scored </string>
    <string name="endgame_textP2"> points in </string>
    <string name="endgame_textP3"> seconds </string>
</resources>

I call the strings with this line in one of my java classes

endLevelLabel.setText(R.string.level_reached_label + level);
endInfoLabel.setText(R.string.endgame_textP1 + score + R.string.endgame_textP2 + gameTime + R.string.endgame_textP3);

The first line runs fine but the second line gives this error (the program works with the 2nd line commented out):

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.web.mathtaketwo/com.web.mathtaketwo.EndGame}: android.content.res.Resources$NotFoundException: String resource ID #0x7d1500be

What did i do wrong that stopped this string from working? Is it because i am trying to use multiple strings in 1 line combined with variables? Why does the other string load fine?

note: the two object that are being set are TextView's:

TextView endInfoLabel = (TextView)findViewById(R.id.endInfoLabel);
TextView endLevelLabel = (TextView)findViewById(R.id.endLevelLabel);

Upvotes: 0

Views: 1075

Answers (1)

JFreeman
JFreeman

Reputation: 1004

Okay so basically the problem is R.string.xxx returns only the ID of a string.

This results in many problems as i try to inserts ID's (integers) into my string.

Instead what i should have done was use the getString() function which returns the string at the id specified:

getString(R.string.xxxx)

If i change my code using getString():

endLevelLabel.setText(getString(R.string.level_reached_label) + (level));
endInfoLabel.setText(getString(R.string.endgame_textP1) + score + getString(R.string.endgame_textP2) + gameTime + getString(R.string.endgame_textP3));

Then it works correctly. For more details see these other topics which i looked at:

Reading value from string resource

String Resources

Get string from resource

Upvotes: 1

Related Questions