Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:03:01+00:00 2026-05-26T04:03:01+00:00

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

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:03:01+00:00Added an answer on May 26, 2026 at 4:03 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a totally noob in writing jUnit tests. I have an Android program
I'm writing my first android app (I'm a noob at android, but decent at
I am a complete noob to android but I have been programing c# for
Overview of the problem: I've been playing with writing custom http server apps for
I am writing an application in Eclipse, that has a servlet, and an android
i'm totally noob to asp.net mvc and currently i'm writing an web app which
Noob question, apologies. I'm compiling Java in Windows Vista's command-line and have so many
noob question on c#: how to create a one-dimensional dynamic array? And how to
Total noob to anything lower-level than Java, diving into iPhone audio, and realing from
Another noob regex problem/question. I'm probably doing something silly so I thought I'd exploit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.