我在我的应用程序中使用 Datepicker。我想要做的是获取已经在文本字段中设置的日期并将其显示在 Datepicker 控件上。但是当我使用时我得到了 NumberFormatException
Integer.parseInt(splittedDate[2]) //i.e the year column.
当我在调试 splittedDate 时看到包含三个很好的值。但是在最后一个索引处解析年份列时会引发异常。我无法理解为什么会这样。请帮我解决这个问题。以下是 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)