Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android resource IDs are being incorrectly assigned

I have encountered a strange problem where if I call getString(R.string.somestringname), from time to time, even after deleting the R file and recompiling, the incorrect string appears. The emulator is not crashing and a valid string is appearing - it's just the wrong one.

A sample of my strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Activity Names -->
    <string name="app_name">an app name</string>
    <string name="selector_name">app name | Select</string>
    <string name="create">Create</string>
    <string name="overview_name">appname | Overview</string>
    <string name="another_name">appname | Another View</string> 

.....
    <!-- Edit Note -->
    <string name="placeholder">Foo bar</string> <-- here is a problem
    <string name="created">Created:</string>

</resources>

Here is a sample of code where one of the problems is encountered:

if (note.created != null) {
     createdtext.setText(getString(R.string.created) + " " + Util.longDateFormat(note.created));
}

The text that is output (some of the time) is Foo bar November 03, 2010 (i.e. belonging to R.string.placeholder). What is going on? This is unnervingly making me tempted to hardcode the strings ;-)

Thanks for your help!

like image 527
SapphireSun Avatar asked Nov 03 '10 05:11

SapphireSun


1 Answers

Use ant clean install instead of just ant install.

Java inlines constants like the R.id values into generated bytecode, and sometimes your classes are not being recompiled, even though they need to be. Ideally, ant install would handle this properly; until it does, ant clean install is your friend.

like image 146
CommonsWare Avatar answered Nov 09 '22 13:11

CommonsWare