Java 如何从 arrays.xml 文件中获取字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2453989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:55:26  来源:igfitidea点击:

How to get String Array from arrays.xml file

javaandroidarrays

提问by Soren

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.

我只是想从我的arrays.xml. 当我尝试在模拟器中运行它时,我收到一条强制关闭消息。

If I define the array in the java file

如果我在java文件中定义数组

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:

这是我的 Java 文件:

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.xmlfile

这是我的arrays.xml文件

<?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>

采纳答案by Dimitar Dimitrov

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

您不能以testArray这种方式初始化您的字段,因为应用程序资源还没有准备好。

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);
    }
}

回答by HaMMeReD

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.

您的 XML 并不完全清楚,但是如果您将数组 XML 设为数字和/或在其定义中放置空格,则它们可能会导致强制关闭。

Make sure they are defined like No Leading or Trailing Whitespace

确保它们被定义为没有前导或尾随空格

回答by Muhammad Aamir Ali

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

您的 array.xml 不正确。改成这样

Here is array.xml file

这是 array.xml 文件

<?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>