开发者

Accessing an xml defined string-array

开发者 https://www.devze.com 2023-04-12 23:17 出处:网络
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

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()
    {
        @O开发者_高级运维verride
        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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号