【问题标题】:How to change a hard coded String Array to a String[] myString = getResources().getStringArray(R.array.myStringArray);如何将硬编码的字符串数组更改为 String[] myString = getResources().getStringArray(R.array.myStringArray);
【发布时间】:2014-07-21 21:09:39
【问题描述】:

我对这个 android eclipse 编程很陌生。我花了几天时间在网上搜索答案,但还没有找到答案。 我已经设法使用片段使列表视图工作,但字符串数据是硬编码的,我想使用资源 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)

我看不到任何行号?

【问题讨论】:

  • 建议您发布您的getResources()getStringArray() 函数,以便我们了解您要执行的操作。乍一看,您可能希望查看某种 XML 解析器,例如 JAXB 提供的用于从您的 XML 文件中获取数据的解析器。
  • @RyanJ 这些函数是 Android API 的一部分

标签: java android arrays eclipse string


【解决方案1】:

您好,您正在将一个常量 static final 更改为无需对象实例即可访问的字段,该字段首先需要一个实例,我建议您将初始化移动到对象的构造函数中

public class Sample {

    String[] message1 ;

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

要告诉您更多信息,您需要粘贴错误消息(编辑:抱歉,更具体地说,我的意思是错误消息为您提供了 NPE 所在的确切行,您可以粘贴确切的行吗?)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2012-06-28
  • 1970-01-01
  • 2013-12-23
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多