Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRodrigo Muñoz
Courses Plus Student 20,171 PointsAndroid Blog Reader > Rebuilding from scratch > All about @string resources
Hi everyone,
I need some help with this @string resource while the eclipse doesn't recognize when adding a new string to the strings.xml file. I can create a new string by typing the new string in the xml view but it doesn't get done if I use the resource window and add a new string. After I typing the new string it neither modify the R.java file so the MainListActivity.java won't recognize the new string to use it in the Toast message and the textView also shows only "textView" in the graphical layout and not the actual message that came from the string.
Here is the code from the MainListActivity.java that doesn't recognizer the string called no_items
String message = getString(R.string.no_items);
Toast.makeText(this, message, Toast.LENGTH_LONG.show();
}
and here is the strings.xml file that can only add a string by typing and not by use the add resource
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BlogReader</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="no_items">No items to display!</string>
</resources>
The only way I could fix this it was by changing the R.java file and adding a new string but the value added it doesn't look like the other strings being used in the project.
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050002;
public static int no_items;
2 Answers
Ben Jakuben
Treehouse TeacherHi @Rodrigo,
While I applaud your effort for trying to change the R.java
file (I remember trying something similar when I first started with Android), that file is generated automatically by the Android tools each time your project is built. So any changes you make will be overwritten.
Let's walk through the process of adding a String step by step. Sometimes it's a simple solution that seems obvious in retrospect!
- Add the string in strings.xml, either by typing or using the Add String wizard.
- Save the changes (I've kicked myself for forgetting to save in the past!)
- Built the project. The project builds automatically by default, but you may have turned that off for some reason.
- Run and test
If those basic steps don't fix it, you might want to try cleaning your project (Project > Clean...) and building and running, or even closing out of Eclipse and trying it all over again. Every once in a while something may get corrupted in your current Eclipse session and restarting is the only way to fix it.
Hope this helps!
Rodrigo Muñoz
Courses Plus Student 20,171 PointsThanks @Ben! I followed these steps and it helped me realize that I missed one parenthesis and it also fixed the string problem. But I do still have the Build Automatically option enable and it does not work when I try to add a string using the resource window view in the strings.xml file