2014-02-14 125 views
2

我试图将string arraylistview_array)转换为int array,然后比较数字。不幸的是,每次执行应用程序都会崩溃。将字符串数组转换为int数组android

下面是代码:

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> 
莫非

任何人提供有关什么可能是问题的想法?

谢谢。

+2

LogCat说什么? – pedromss

+3

您是否将“05:40”或“05”和“40”传递给Interger.parseInt?传递“05:40”不起作用。 –

回答

1

我认为你的问题是当你尝试转换值时,因为像这样的“05:40”这样的值不能被转换成int。

可能,您会得到一个NumberFormatException

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

+0

谢谢。这非常有帮助,我可以解决这个问题! – user3302071