Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing an xml defined string-array
    primarykey
    data
    text
    <p>I am a noob at writing Android apps, I have a problem using <code>getResources().getStringArray()</code>.</p> <p>I make an <code>string-array</code> resource from Eclipse (or by hand) in a file called <em>res.values.strings.xml</em> looking like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string-array name="BlueStartCommands"&gt; &lt;item&gt; NO_CMD&lt;/item&gt; &lt;item&gt; LOCK_CMD&lt;/item&gt; &lt;item&gt; UNLOCK_CMD&lt;/item&gt; &lt;item&gt; TRUNK_RELEASE_CMD&lt;/item&gt; &lt;item&gt; PANIC_CMD&lt;/item&gt; &lt;item&gt; REMOTE_START_CMD&lt;/item&gt; &lt;item&gt; REQ_START_MONITOR_CMD&lt;/item&gt; &lt;item&gt; REQ_PASSIVE_LOCK_SETTINGS_CMD&lt;/item&gt; &lt;item&gt; REQ_STOP_MONITOR_CMD&lt;/item&gt; &lt;item&gt; PING_CMD&lt;/item&gt; &lt;/string-array&gt; &lt;string-array name="CMD"&gt; &lt;item&gt;AAAAAA&lt;/item&gt; &lt;item&gt;BBBBBB&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt; </code></pre> <p>I then try to access the array in my <code>Activity</code>. It crashes:</p> <pre><code>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&lt;?&gt; 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&lt;?&gt; arg0) { // TODO Auto-generated method stub } }); } </code></pre> <p>However, when I use the code created <code>array-string</code> <em>cmd</em>, it works! Why is that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload