0

I have following code:

String personalinfos[] = {"Age", "Gender", "Height", "Weight"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myLayout);

setListAdapter(new ArrayAdapter<String>(Screening.this, android.R.layout.simple_list_item_1, personalinfos));

And it works, it does it's job. But as I need to do internationalized / localized project, I'm moving it to strings.xml, so I've added this to strings.xml

<string-array name="my_keys">
    <item>Age</item>
    <item>Gender</item>
    <item>Height</item>
    <item>Weight</item>
</string-array>

And tried to change code into:

String personalinfos[] = getResources().getStringArray(R.array.my_keys);

Assuming that I'll get same result, but I don't, my app crashes.

So question here:

What's the proper way to read string-array from strings.xml?

I do not understand why it crashes.

Balkyto
  • 1,460
  • 4
  • 22
  • 47
  • Try to declare your `personalinfos[]` as Global variable and initialize it. – Praveenkumar Aug 31 '12 at 13:26
  • 2
    Make sure you're calling getResources().getStringArray(R.array.my_keys); in the onCreate. You can't call getResources() before your activity class is instantiated – Mario Aug 31 '12 at 13:26

5 Answers5

1

Just try like this -

String personalinfos[];
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myLayout);

    personalinfos = getResources().getStringArray(R.array.my_keys);

setListAdapter(new ArrayAdapter<String>(Screening.this, android.R.layout.simple_list_item_1, personalinfos));

Read your related one - Help in getting String Array from arrays.xml file

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
0

Have you tried:

Resources res = getResources();
String[] personalinfos = res.getStringArray(R.array.my_keys);  // no []

Documentation here: http://developer.android.com/guide/topics/resources/string-resource.html

RossC
  • 1,200
  • 2
  • 11
  • 24
0

try to define your strings one by one in strings.xml, then define your items in array like:

<item>@string/string_identifier</item>

is it working?

more info about strings and string arrays here

Berťák
  • 7,143
  • 2
  • 29
  • 38
0

1) POST THE LOGCAT. That's why it crashes.

2) What about creating an array.xml file with your arrays and call them directly from your xml?

So create your array.xml and define it like:

<resources>
    <string-array name="array">
        <item>A</item>
        <item>B</item>
    </string-array>
</resources>

and then in your layout use the entries property:

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:entries="@array/array"/>
Enrichman
  • 11,157
  • 11
  • 67
  • 101
0

You can't use getResources() outside any activity/fragment lifetime methods. The resources are not yet ready when you try to access them in your class. To do it properly define your variable anywhere you want and initialize it in onCreate() or onCreateView() methods. See Praveenkumar answer for a simple example.

Variag
  • 1,056
  • 10
  • 25