我在我的應用程序中使用Datepicker。我想要做的是獲取已設置在文本字段中的日期,並在Datepicker控件上顯示該日期。但我得到NumberFormatException當我使用Integer.parseInt拋出NumberFormatException?
Integer.parseInt(splittedDate[2]) //i.e the year column.
當我看到,而調試splittedDate containf三個值都很好。但在解析年份列的最後一個索引時拋出異常。我無法理解爲什麼會這樣。請幫我解決這個問題。以下是Datepicker的CreateDialog方法的實現。
@Override
protected Dialog onCreateDialog(int id) {
if (id == DATE_DIALOG_ID) {
if (birthDate.getText().toString().equalsIgnoreCase(""))
return new DatePickerDialog(this, mDateSetListener, mYear,
mMonth, mDay);
else {
String[] splittedDate = birthDate.getText().toString().split("-");
try{
Log.i("Month String", splittedDate[0]);
mMonth = Integer.parseInt(splittedDate[0]);
Log.i("Day String", splittedDate[1]);
mDay = Integer.parseInt(splittedDate[1]);
Log.i("Year String", splittedDate[0]);
mYear = Integer.parseInt(splittedDate[2]);
}
catch(Exception e)
{
Log.e("Number Format Exception Message: ",e.getMessage());
}
return new DatePickerDialog(this, mDateSetListener, mYear,
mMonth, mDay);
}
return null;
}
這裏是logcat的信息:
09-05 16:21:54.722: E/AndroidRuntime(28596): FATAL EXCEPTION: main
09-05 16:21:54.722: E/AndroidRuntime(28596): java.lang.NumberFormatException: unable to parse '1970 ' as integer
09-05 16:21:54.722: E/AndroidRuntime(28596): at java.lang.Integer.parse(Integer.java:383)
09-05 16:21:54.722: E/AndroidRuntime(28596): at java.lang.Integer.parseInt(Integer.java:372)
09-05 16:21:54.722: E/AndroidRuntime(28596): at java.lang.Integer.parseInt(Integer.java:332)
09-05 16:21:54.722: E/AndroidRuntime(28596): at com.test.project.activity.DetailActivity.onCreateDialog(DetailActivity.java:332)
你能發佈導致異常的字符串嗎? – MByD
可否請上傳stacktrace – harshit
它基本上是splittedDate數組中的第三個元素......它的值是1970 –