Organiccat
Organiccat

Reputation: 5651

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>

Upvotes: 12

Views: 17616

Answers (3)

Alberto
Alberto

Reputation: 71

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);

Upvotes: 6

BradVoy
BradVoy

Reputation: 717

I've run into similar problems several times. I found that using the Project | Clean option in Eclipse and then rebuilding my project always solves them. That's essentially what you're doing by editing the XML file; you're forcing Eclipse to rebuild its resources from the XML file.

Upvotes: 2

Organiccat
Organiccat

Reputation: 5651

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.

Upvotes: 11

Related Questions