[java] How to get String Array from arrays.xml file

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>

This question is related to java android arrays

The answer is


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


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>

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?