Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get array string resource from XML

I have a list of values in my XML file which I would like to select based off a spinner selection. For some reason, using an array-string for the spinner works fine, the values are populated into the spinner. For whatever reason, I cannot get the values of the second array to save my life, they are in the same file which has no errors that I can find. Here is the way I am trying to grab them:

String[] some_array = getResources().getStringArray(R.array.playerclassdesc_array);

The spinner is populated differently (no errors doing this part):

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    this, R.array.playerclass_array, android.R.layout.simple_spinner_item);

The error I am getting is:

"04-26 21:41:35.305: ERROR/AndroidRuntime(514): Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x7f050001"

Which refers directly to the "getResources" line. Does anyone have any clue why this could be happening?

Edit: Here is the xml file (it's simple)

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <string name="class_prompt">Choose a class</string>
    <string-array name="playerclass_array">
        <item>Assassin</item>
        <item>Paladin</item>
        <item>Pirate</item>
        <item>Hell Mage</item>
        <item>Winter Witch</item>
        <item>Shadow Walker</item>
        <item>Underthief</item>
        <item>Red Warrior</item>
        <item>Haru Norda</item>
    </string-array>
    <string-array name="playerclassdesc_array">
        <item>This is the Assassin</item>
        <item>This is the Paladin</item>
        <item>This is the Pirate</item>
        <item>This is the Hell Mage</item>
        <item>This is the Winter Witch</item>
        <item>This is the Shadow Walker</item>
        <item>This is the Underthief</item>
        <item>This is the Red Warrior</item>
        <item>This is the Haru Norda</item>
    </string-array>
</resources>
like image 252
Organiccat Avatar asked Apr 26 '11 21:04

Organiccat


People also ask

In which folder can you find the string resource file strings XML?

The XML resource files containing localized strings are placed in subfolders of the project's res folder.

Where is strings XML in Android Studio?

XML file saved at res/values/strings.

Does XML have arrays?

XML can contain an array with varying numbers of elements. In general, WSDL documents and XML schemas that contain varying numbers of elements do not map efficiently into a single high-level language data structure. CICS® uses container-based mappings or inline mappings to handle varying numbers of elements in XML.


2 Answers

I hate it when I find the answer right after!

For some reason, Android was not properly recompiling the XML file the array was in, so after changing the name, and then changing it back, it now works. So my solution is that I had to make some edits in the XML file the array resided in because it wasn't recompiling correctly despite Eclipse telling me it was.

like image 64
Organiccat Avatar answered Sep 29 '22 16:09

Organiccat


I try to do a Cast in the constructor of ArrayAdapter, or chage the type of the element in ArrayAdapter.

ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(
this, R.array.playerclass_array, android.R.layout.simple_spinner_item);
like image 44
Alberto Avatar answered Sep 29 '22 16:09

Alberto