2

I was poking around in strings.xml, and I added a string: "itemTag":

<string name="itemTag">1</string>

When I tried to access the string in an activity via R.string.itemTag, it popped up an error:

itemTag cannot be resolved or is not a field

I looked into the R.java file, and I couldn't find the string. Is there a reason why it wasn't added in the file when I added the string in string.xml and how can I fix this?

Sorry if this is a duplicate, I couldn't find anything on Google.

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="itemTag">1</string>
</resources>

EDIT:

I'm not sure if most of the viewers understand, in the R.java where all of the ids and strings are located (I assume), I cannot find itemTag. It is simply not there. I am assuming that once I create a string in string.xml, the string would also automatically pop up in R.java, but it doesn't seem like that is the case.

7
  • 1
    Try to clean your project (Project > Clean > Select your project) Commented Apr 10, 2014 at 22:07
  • Tried that, it deleted the R.java and didn't rebuild itself. Commented Apr 10, 2014 at 22:08
  • Are you getting an error on that line in your xml? Or in any other xml? Commented Apr 10, 2014 at 22:08
  • I'm getting the error on an activity Commented Apr 10, 2014 at 22:08
  • Can you please share your full strings.xml file? Commented Apr 10, 2014 at 22:13

6 Answers 6

1

Use the getString method:

String itemTagStr = getString(R.string.itemTag);
Sign up to request clarification or add additional context in comments.

7 Comments

Same error, its because it can't find "itemTag" as it's not stated in R.java I believe
What happens if you comment out the getString() line temporarily, then clean and save to allow for a successful build. Will the string resource then show up?
Even if I comment out the getString() line, cleaning just deletes the R.java file and doesn't regenerate it back.
Is there any other place in the java code that is attempting to access R.string.itemTag?
I'm not sure if you understand, in the R.java where all of the ids and strings are located (I assume), I cannot find itemTag. It is simply not there. I am assuming that once I create a string in string.xml, the string would also automatically pop up in R.java, but it doesn't seem like that is the case.
|
1

You will need to clean as well as build the project. During the build, R.java should be regenerated and your changes should show up.

4 Comments

I did clean and rebuild the project, but R.java does not regenerate
Can you also post how you are building? Sometimes when you edit xml file in some other editor, eclipse doesnt pickup changes in file and you need to refresh project. There can possibly be other similar reasons. Can you post some info on how you are editing and building?
I did not use any other editor, and I edited the strings.xml using eclipse and in that xml file. When I go into an activity and type R.string then Crtl + space (which shows the possible strings), nothing shows up except this and class.
I wonder if your project structure or classpath is somehow messed up. I would try recreating the project and see. Or may be just eclipse restart.
1

Make sure that your string.xml is in res>values>string.xml. Else clean up your project and reopen the IDE you are using. It will work.

2 Comments

Did all that, and it didn't work. After cleaning the project R.java was not regenerated.
Where are you using the value of this string? Can you show the code?
1

Make sure android.R is not imported into your project. Delete this import and import in your actual R file.

Eclipse has a tendency to automatically import android.R when a new Android project is created.

Comments

0

Delete your R.java file. Go to Activity_main.xml and build it for API 17, i.e. Android 4.2.2 . Now do a 'Clean' and see your R.java will regenerate and you'll find your newly added strings in the file

Comments

0

I created a string in string.xml as

<string name="itemTag">1</string>

Then rebuild the project and I found it in R.java

public static final int itemTag=0x7f0b0021;

And finally I used it here and there is no error

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText((//Context:) this, (//String) R.string.itemTag, Toast.LENGTH_SHORT).show();
}

I hope this will help.

1 Comment

You should provide codes here instead of images of codes. Please edit your post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.