Kotlin(null 不能转换为非 null 类型)自定义日历实现
Kotlin (null cannot be cast to non-null type) Custom Calendar Implementation
我正在尝试在 Java 中使用库 https://github.com/kizitonwose/CalendarView(它是用 Kotlin 制作的)。在这个库中有一个 select 日期的例子。在示例中,它已显示这样做:
private fun selectDate(date: LocalDate) {
if (selectedDate != date) {
val oldDate = selectedDate
selectedDate = date
oldDate?.let { exThreeCalendar.notifyDateChanged(it) }
exThreeCalendar.notifyDateChanged(date)
updateAdapterForDate(date)
}
}
在我自己的 android 项目中尝试复制它之后,我写了这个:
private void selectDate(LocalDate date){
if(selectedDate != date){
// oldDate is null
// date is not null
LocalDate oldDate = selectedDate;
selectedDate = date;
if (oldDate != null){
calendarView.notifyDateChanged(oldDate);
}
calendarView.notifyDateChanged(date);
updateAdapterForDate(date);
}
}
当我的片段启动时,它调用 selectDate() 方法并将今天的日期作为参数(不为空)。
它在
上给出错误
calendarView.notifyDateChanged(date);
说
null cannot be cast to non-null type com.kizitonwose.calendarview.ui.CalendarAdapter
我想知道如何实现相同的结果
oldDate?.let { exThreeCalendar.notifyDateChanged(it) }
in Java 或者如果我的代码有其他不正确的地方。
感谢您的帮助。
注意错误消息中的类型。它不是 date
是 null,而是 CalendarAdapter
类型的东西,我怀疑
private val calendarAdapter: CalendarAdapter
get() = adapter as CalendarAdapter
看起来adapter
应该设置在setup
,也许你没有调用它?
我正在尝试在 Java 中使用库 https://github.com/kizitonwose/CalendarView(它是用 Kotlin 制作的)。在这个库中有一个 select 日期的例子。在示例中,它已显示这样做:
private fun selectDate(date: LocalDate) {
if (selectedDate != date) {
val oldDate = selectedDate
selectedDate = date
oldDate?.let { exThreeCalendar.notifyDateChanged(it) }
exThreeCalendar.notifyDateChanged(date)
updateAdapterForDate(date)
}
}
在我自己的 android 项目中尝试复制它之后,我写了这个:
private void selectDate(LocalDate date){
if(selectedDate != date){
// oldDate is null
// date is not null
LocalDate oldDate = selectedDate;
selectedDate = date;
if (oldDate != null){
calendarView.notifyDateChanged(oldDate);
}
calendarView.notifyDateChanged(date);
updateAdapterForDate(date);
}
}
当我的片段启动时,它调用 selectDate() 方法并将今天的日期作为参数(不为空)。 它在
上给出错误calendarView.notifyDateChanged(date);
说
null cannot be cast to non-null type com.kizitonwose.calendarview.ui.CalendarAdapter
我想知道如何实现相同的结果
oldDate?.let { exThreeCalendar.notifyDateChanged(it) }
in Java 或者如果我的代码有其他不正确的地方。 感谢您的帮助。
注意错误消息中的类型。它不是 date
是 null,而是 CalendarAdapter
类型的东西,我怀疑
private val calendarAdapter: CalendarAdapter
get() = adapter as CalendarAdapter
看起来adapter
应该设置在setup
,也许你没有调用它?