Balkyto
Balkyto

Reputation: 1510

Android string works from code but crashes from string.xml

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.

Upvotes: 0

Views: 437

Answers (5)

Variag
Variag

Reputation: 1085

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.

Upvotes: 0

Enrichman
Enrichman

Reputation: 11337

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"/>

Upvotes: 0

Berť&#225;k
Berť&#225;k

Reputation: 7322

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

Upvotes: 0

Praveenkumar
Praveenkumar

Reputation: 24476

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

Upvotes: 1

RossC
RossC

Reputation: 1230

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

Upvotes: 0

Related Questions