如何将硬编码的String数组更改为String [] myString = getResources()。getStringArray(R.array.myStringArray);

时间:2014-07-21 21:09:39

标签: java android arrays eclipse string

我对这个android eclipse编程很新。我花了几天时间在网上搜索答案,但还没有找到答案。 我设法使用片段获取listview,但字符串数据是硬编码的,我想使用资源xml文件中的字符串。

这是硬编码数据

public static final String[] message1 = new String[] { "Strawberry",
       "Banana", "Orange", "Mixed" };

这是我的字符串 //为行数据message1显示的字符串数组数据

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

来自我的strings.xml文件

<string-array name="main_page_list_string">
    <item >Instruments</item>
    <item >Capasiters</item>
    <item >Resisters</item>
    <item >Ohms Law</item>
    <item >IP Ratings</item>
    <item>Thermostats</item>
    <item>Converters</item>
    <item >RJ 45 A and B plugs</item><item>Calculators</item>
</string-array>

每次我替换

public static final String[] message1 = new String[] { "Strawberry",
       "Banana", "Orange", "Mixed" };

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

我的应用程序崩溃,错误消息为空点异常。

谁能告诉我怎么做?enter code here 感谢

这是java类文件

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ImageTextListBaseAdapterActivity extends Activity implements
OnItemClickListener {

  ***********************************************************************************
  This is the code i am trying to use
    //String array data to display for row data message1
    String[] myStringData=getResources().getStringArray(R.array.main_page_list_string); 

    This is the code that i am trying to replace 

   ********************************************************************************  
    //    public static final String[] message1 = new String[] { "Strawberry",
    //        "Banana", "Orange", "Mixed" };
   ******************************************************************************
    //String array data to display for row data message2
    public static final String[] descriptions = new String[] {
        "It is an aggregate accessory fruit",
        "It is the largest herbaceous flowering plant", "Citrus Fruit",
    "Mixed Fruits" };

    //icons used in list item rows
    public static final Integer[] images = { R.drawable.ic_launcher,
        R.drawable.songs_gray, R.drawable.videos_gray, R.drawable.photos_gray };

    ListView listView;
    List<RowItem> rowItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_mainlist);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < message1.length; i++) {
            RowItem item = new RowItem(images[i], message1[i], descriptions[i]);
            rowItems.add(item);
        }

        listView = (ListView) findViewById(R.id.MainPageListView);
        CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Toast toast = Toast.makeText(getApplicationContext(),
                "Item " + (position + 1) + ": " + rowItems.get(position),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }
}

这是LogCat的错误

07-22 08:41:12.659:E / AndroidRuntime(13799):致命异常:主要 07-22 08:41:12.659:E / AndroidRuntime(13799):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.rogerssteve.android.elecinstro / com.rogerssteve.android.elecinstro.ImageTextListBaseAdapterActivity}:java.lang 。空指针异常 07-22 08:41:12.659:E / AndroidRuntime(13799):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)

我看不到任何行号?

1 个答案:

答案 0 :(得分:1)

您好,您正在更改一个可以在没有对象实例的情况下访问的常量static final到一个首先请求实例的字段我建议将您的初始化移动到您的对象的构造函数

public class Sample {

    String[] message1 ;

    public Sample(){
         message1 = getResources().getStringArray(R.array.main_page_list_string);
    }
}

告诉你更多你需要粘贴你的错误信息(编辑:对不起,更具体的是我的意思是错误信息给你一个确切的行NPE,你可以粘贴确切的行吗?)