【发布时间】:2014-03-08 16:06:06
【问题描述】:
我是 Android 编程新手,正在学习 Coursera 课程。在我的作业应用程序中,当用户未输入或输入不正确的时间信息 (HH:MM:SS) 时,我试图捕捉任何异常。
我的应用程序在用户出错后立即崩溃,而不是显示我的 Toast 消息或向 Logcat 显示日志消息。
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try {
Date dt = formatter.parse(MileTime);
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int hours = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int duration = 3600 * hours + 60 * minutes + seconds;
int steps_per_second = 3;
int running_rate = duration * steps_per_second;
//pleaseStop = false; //DouglasZare example, don't need this.
mHandler = new Handler(); // .os package class when importing
mLeftfoot = findViewById(R.id.leftfoot);
mRightfoot = findViewById(R.id.rightfoot);
mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot); //this looks to the foot.xml file for the animations
stepRecursive();
} catch (ParseException e) {
e.printStackTrace();
Log.e(TAG, "Invalid Mile Time Entry", e);
Toast.makeText(Assignment3MainActivity_V3_DouglasZare_AnimationCancel.this,
"Please Enter Valid Time Stamp HH:MM:SS", Toast.LENGTH_LONG).show();
}
错误日志在这里:http://pastebin.com/By7FWxLk。
这个错误非常合理,而且是故意的。
Caused by: java.text.ParseException: Unparseable date: ""
我该如何解决才能让我的 catch 语句防止这种情况发生?
【问题讨论】:
标签: java android exception try-catch