我正在 a 中显示日期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);
Run Code Online (Sandbox Code Playgroud)
这是导致崩溃的代码:
if (tv_Current_Date.getText().toString().isEmpty()) {
til_Current_Date.setError("Please choose a date");
}
Run Code Online (Sandbox Code Playgroud)
设置日期的方法:
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);
}
Run Code Online (Sandbox Code Playgroud)