How to get String Array from arrays.xml file

JavaAndroidArrays

Java Problem Overview


I am just trying to display a list from an array that I have in my arrays.xml. When I try to run it in the emulator, I get a force close message.

If I define the array in the java file

String[] testArray = new String[] {"one","two","three","etc"};

it works, but when I use

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

it doesnt work.

Here is my Java file:

package com.xtensivearts.episode.seven;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Episode7 extends ListActivity {
 String[] testArray = getResources().getStringArray(R.array.testArray);
 
 /** Called when the activity is first created. */
 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  // Create an ArrayAdapter that will contain all list items
  ArrayAdapter<String> adapter;
  
  /* Assign the name array to that adapter and 
     also choose a simple layout for the list items */ 
  adapter = new ArrayAdapter<String>(
    this,
    android.R.layout.simple_list_item_1,
    testArray);
  
  // Assign the adapter to this ListActivity
  setListAdapter(adapter);
 }

 
}

Here is my arrays.xml file

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  <array name="testArray">  
    <item>first</item>  
    <item>second</item>  
    <item>third</item>  
    <item>fourth</item>  
    <item>fifth</item>  
  </array>
</resources>

Java Solutions


Solution 1 - Java

You can't initialize your testArray field this way, because the application resources still aren't ready.

Just change the code to:

package com.xtensivearts.episode.seven;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Episode7 extends ListActivity {
    String[] mTestArray;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Create an ArrayAdapter that will contain all list items
        ArrayAdapter<String> adapter;

        mTestArray = getResources().getStringArray(R.array.testArray);    

        /* Assign the name array to that adapter and 
        also choose a simple layout for the list items */ 
        adapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            mTestArray);

        // Assign the adapter to this ListActivity
        setListAdapter(adapter);
    }
}

Solution 2 - Java

Your array.xml is not right. change it to like this

Here is array.xml file

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string-array name="testArray">  
        <item>first</item>  
        <item>second</item>  
        <item>third</item>  
        <item>fourth</item>  
        <item>fifth</item>  
   </string-array>
</resources>

Solution 3 - Java

Your XML is not entirely clear, but arrays XML can cause force closes if you make them numbers, and/or put white space in their definition.

Make sure they are defined like No Leading or Trailing Whitespace

Solution 4 - Java

You can also get the string array from xml as ArrayList by the following code

xml array:

    <string-array name="testArray">  
        <item>first</item>  
        <item>second</item>  
        <item>third</item>  
        <item>fourth</item>  
        <item>fifth</item>  
   </string-array>

kotlin code:

val stringArray: ArrayList<String> =  resources.getStringArray(R.array.testArray).toList() as ArrayList<String>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSorenView Question on Stackoverflow
Solution 1 - JavaDimitar DimitrovView Answer on Stackoverflow
Solution 2 - JavaMuhammad Aamir AliView Answer on Stackoverflow
Solution 3 - JavaHaMMeReDView Answer on Stackoverflow
Solution 4 - Javarama-voidView Answer on Stackoverflow