2

I'm getting an error about a string array (located in strings.xml) not being able to be resolved.

I've done everything the internet says:

  1. Restarted Eclipse
  2. Deleted my R.java file inside the gen folder
  3. My values folder is still inside the res folder, not moved
  4. None of my XML files have erros
  5. Ran Project -> Clean
  6. I don't have android.R imported in my class.
  7. Checked the R.java class, it indeed doesn't have an app_categories property (my array's name)

The relevant part of the XML:

<string-array name="app_categories">
    <item >Cat 1</item>
    <item >Cat 2</item>
    <item >Cat 3</item>
    <item >Cat 4</item>
    <item >Cat 5</item>
    <item >Cat 6</item>
    <item >Cat 7</item>
    <item >Cat 8</item>
    <item >Cat 9</item>
</string-array>

The Java code:

String[] categoryNames = getResources().getStringArray(R.string.app_categories);

All my other strings are visible. I have another array defined just like this one, and that one isn't visible either.

Exact error below:

app_categories cannot be resolved or is not a field [class_name].java   [path]  line 45 Java Problem
8
  • 1
    @Abu Man, I hope that doesn't work, since it's XML, and XML shouldn't care about non-significant whitespace. Commented Jan 16, 2013 at 13:36
  • Nope, it doesn't. Fortunately for the world's sake and unfortunately for me. Commented Jan 16, 2013 at 13:37
  • what error does it throw? can you paste that? Commented Jan 16, 2013 at 13:38
  • Check the import of "R" try to use package.R instead. Commented Jan 16, 2013 at 13:38
  • 1
    The resource name should begin with R.array, not R.string. Commented Jan 16, 2013 at 13:45

4 Answers 4

9

Just try out this way :

 getResources().getString(R.array.app_categories_list);

Instead of R.string use R.array you will get your arraylist.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a God! Thank you. I thought the pattern was R.[singular_of_file_name_in_which_resource_is].[resource_name], but it's obviously not.
4

Don't put your string arrays in strings.xml You must create a new file called arrays.xml and place them there. And when calling you need R.arrays.myarray, not R.strings.MyArray

3 Comments

This is the solution I was going to post (you beat my by 30 seconds). This is how I have my arrays setup and it works fine for me.
This helped me realize what the problem was, but it wasn't the solution for the problem per-se. I was calling R.string.app_categories, but I should have called R.array.app_categories. +1 for that
Type-specific naming of resource file doesn't matter. Its the xml tags which does.
0

After cleaning your project try restarting the adb server by running adb kill-server and adb start-server from the command line

Comments

0

a solution may be to create the R manually:

just hover over the error in the activity file where the problem was and then manually create it, which will cause R to generate the string array you created in strings.xml

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.