How to get String Array from arrays.xml file

I'm 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 power message.

If I define an array in a java file

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

it works but when i use

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

he does not 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> 
+92
java android arrays
Mar 16 '10 at 11:45
source share
3 answers

You cannot initialize the testArray field this way because application resources are still not 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); } } 
+179
Mar 16 '10 at 11:59
source share

Your .xml array is invalid. change it like this:

Here is the 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> 
+26
Nov 27 '12 at 7:24
source share

Your XML is not entirely clear, but XML arrays can lead to the closure of the force if you make them numbers and / or put empty space in your definition.

Make sure they are defined as No Leading or Trailing Whitespace

+2
Dec 17 '10 at 22:57
source share



All Articles