将字符串数组转换为int数组android

时间:2014-02-14 22:56:25

标签: java xml arrays string int

我正在尝试将string arraylistview_array)转换为int array,然后比较数字。不幸的是,每次执行时,应用程序都会崩溃。

以下是代码:

public class FindStop3 extends Activity implements OnItemClickListener{
    private String stop,line,listview_array[],buschain,dayweek,weekly;
    private int selected,day;   
    private TextView tvLine,tvToday,tvNext;
    private ListView lv;    
    String currentdate = java.text.DateFormat.getDateInstance().format(Calendar.getInstance().getTime());//Get current time
    String currenttime = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());//Get current time
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_stop3);
        FindStop3.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);//Lateral transition

        Intent intent = getIntent();//Take data from last activity
        Bundle bundle = intent.getExtras();
        line=bundle.getString("Line");//Takes the previous selected line from the last activity
        stop=bundle.getString("Stop");//Takes the previous selected stop from the last activity
        setTitle("Selected stop: "+stop);//Modifies title

        getDayOfWeek();//Gets day of week
        getBusChain(line,stop,weekly);//Calls method to get the xml chain name        

        tvLine = (TextView) findViewById(R.id.tvLine);
        tvLine.setText("Line from "+line);

        tvNext = (TextView) findViewById(R.id.tvNext);
        tvNext.setText("Next bus arrives at "+currenttime);


        tvToday = (TextView) findViewById(R.id.tvToday);
        tvToday.setText(dayweek+","+currentdate+" schedule:");          

        selected= getResources().getIdentifier(buschain, "array",
                this.getPackageName());//Lets me use a variable as stringArray

        listview_array = getResources().getStringArray(selected);

        lv = (ListView) findViewById(R.id.lvSchedule);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));


        getNextBus();


    }

以下是转换它的方法:

public void getNextBus () {
        //Converts listview_array into an integer array

         int myar[]=new int[listview_array.length];

                  for(int i=0;i<listview_array.length;i++){

                      myar[i]=Integer.parseInt(listview_array[i]);



            }

如果未执行该方法,则应用程序可以正常运行。这些值取自xml文件,如下所示:

<string-array name="Schedule">
        <item>05:40</item>
        <item>06:00</item>
        <item>06:16</item>
        <item>06:28</item>
        <item>06:40</item>
        <item>07:16</item>
        <item>07:29</item> 
        <item>07:43</item>
        <item>07:55</item>
        <item>08:07</item>
        <item>08:22</item>       
    </string-array>

有人能说出可能出现的问题吗?

感谢。

1 个答案:

答案 0 :(得分:1)

我认为您的问题是当您尝试转换值时,因为像“05:40”这样的值无法转换为int。

可能的是,你得到的是 NumberFormatException

如果您想要更好的答案,请发送您应用的日志。