0

I am a noob at writing Android apps, I have a problem using getResources().getStringArray().

I make an string-array resource from Eclipse (or by hand) in a file called res.values.strings.xml looking like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="BlueStartCommands">     
    <item> NO_CMD</item>        
    <item> LOCK_CMD</item>
    <item> UNLOCK_CMD</item>
    <item> TRUNK_RELEASE_CMD</item>
    <item> PANIC_CMD</item>
    <item> REMOTE_START_CMD</item>
    <item> REQ_START_MONITOR_CMD</item>
    <item> REQ_PASSIVE_LOCK_SETTINGS_CMD</item>
    <item> REQ_STOP_MONITOR_CMD</item>
    <item> PING_CMD</item>             
</string-array>
<string-array name="CMD">
    <item>AAAAAA</item>
    <item>BBBBBB</item>
</string-array>
</resources>

I then try to access the array in my Activity. It crashes:

public class TestApp2Activity extends Activity {
    static boolean firstWrite=true;
    TextView DebugWindow;
        ViewFlipper flipper;
    Button FlipBtn; 
    Button SwitchBtn;
    EditText MessageBox;
    Spinner PredefinedMessages;
    OnItemSelectedListener PredefinedMessageSelected;
    int SelectedPredefinedMessage=0;
    String[] MyPredefinedCommands = {"fsd", "fdg", "sdf", "saf", "w", "v", "u", "t", "z", "y", "x"};    
    String[] cmd = getResources().getStringArray(R.array.CMD);
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle icicle) {               
    super.onCreate(icicle);
    setContentView(R.layout.main);      
    flipper=(ViewFlipper)findViewById(R.id.details);
    FlipBtn=(Button)findViewById(R.id.Button01);
    SwitchBtn=(Button)findViewById(R.id.Button04);
    DebugWindow=(TextView)findViewById(R.id.textView1);
    MessageBox=(EditText)findViewById(R.id.editText2);
    PredefinedMessages=(Spinner)findViewById(R.id.spinner1);
    PredefinedMessages.setPrompt("Please enter a predefined command");
    @SuppressWarnings("rawtypes")
    ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.BlueStartCommands , android.R.layout.simple_spinner_item);        
    PredefinedMessages.setAdapter(adapter);                 

    PredefinedMessages.setOnItemSelectedListener(new ListView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> a, View v, int i, long l) {
            try {
                // Remembers the selected Index
                SelectedPredefinedMessage = i;                  
                String s = MyPredefinedCommands[i];
                System.out.print(s);
                MessageBox.setText(cmd[i]);
            }
            catch(Exception e) {
                System.out.println("Nay, cannot get the selected index");
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub              
        }
    });

    }

However, when I use the code created array-string cmd, it works! Why is that?

4
  • What ever resource we use in our app android framework creates static reference to them in R.java whole strings.xml is converted to objects when you call getresource(id) it is something like DOM not exactly. Commented Oct 13, 2011 at 12:38
  • And so? What is the mistake? Sorry, I don't get it (yet!). Commented Oct 13, 2011 at 12:42
  • can you post the error message you get? Commented Oct 13, 2011 at 12:46
  • That's the problem with Android, actually, so far, all I get are crashes...ofc I use LogCat, but still it is unnerving to see your app crash at every little step...grr... See below for correct fix! Commented Oct 13, 2011 at 12:59

2 Answers 2

3

I think the problem is that you are trying to retrieve your array before onCreate() is called (you're assigning it in your global declaration).
Try String[] cmd; in the class where you are declaring it now,
and cmd = getResources().getStringArray(R.array.CMD); in the onCreate() function.

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

1 Comment

Thank you! That did the trick! Silly noob mistake, but, it happened...(wow for the response times btw..)
0

I have defined my arrays in res/values/arrays.xml and access them with:

CharSequence[] monthArray = getResources().getStringArray(R.array.Month);
List<CharSequence> monthArrayList = Arrays.asList(monthArray);

Comments

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.