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.

2
  • Try to declare your personalinfos[] as Global variable and initialize it. Commented Aug 31, 2012 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 Commented Aug 31, 2012 at 13:26

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, the related link helped + combination of Berťák answer to create a String list.
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

Comments

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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.