【发布时间】:2018-12-03 18:31:33
【问题描述】:
我在TextView 中显示日期。有日期时一切正常。但是,如果没有选择日期或者TextView 为空(尽管有提示“dd-mm-yyyy”),那么应用程序就会崩溃。我正在检查空的TextView 如下:if(textview.setText().toString().isEmpty()) {//show error} 谁能帮助我做错了什么?
TextView和TextInputlayout的初始化:
tv_Current_Date = (TextView) findViewById(R.id.tv_Current_Date);
til_Current_Date = (TextInputLayout) findViewById(R.id.til_Current_Date);
这是导致崩溃的代码:
if (tv_Current_Date.getText().toString().isEmpty()) {
til_Current_Date.setError("Please choose a date");
}
设置日期的方法:
public void setCurrentDateOnView() {
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
tv_Current_Date = (TextView) findViewById(R.id.tv_Current_Date);
tv_Current_Date.setText(simpleDateFormat.format(calendar_now.getTime()));
String short_weekday = new DateFormatSymbols().getShortWeekdays()[day_of_current_week];
tv_Current_weekday.setText(short_weekday);
}
【问题讨论】:
-
还要添加您定义 til_Current_Date 的部分,以防您遇到问题。
-
你收到了什么崩溃?
-
我已经编辑了我的代码,@Juan。该应用程序正在停止-an_droid_dev。
-
对不起,实际上我在 setCurrentDateOnView() 方法中本地初始化了 TextView,但我必须全局初始化它。我的代码现在工作正常。谢谢@Juan 指出我的错误。
标签: java android date textview