kotlin - LocalDate.format 在观察者中导致 OutofBount 异常
问题描述
我正在使用 Kotlin、Material Design Components 和新的架构组件在 android 中制作应用程序。我有一个启动 DialogFragment onCreate 的活动
该片段有 6 个视图,通过观察者,每个视图都观察到不同的 LiveDate。
在检查所有这些设置工作时,我注意到在 7 次视图切换后我得到了
2020-05-12 20:43:19.346 4778-4778/package E/InputEventReceiver: Exception dispatching input event.
2020-05-12 20:43:19.346 4778-4778/package E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2020-05-12 20:43:19.357 4778-4778/package E/MessageQueue-JNI:
java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
at android.text.Layout$HorizontalMeasurementProvider.get(Layout.java:1589)
...
我检查了以下内容:
- 仅在一个视图上完成了所有设置 -> 仍然崩溃
- 只在一个视图上完成了所有设置,但没有使用“createDateFieldObserver”方法-> 仍然是 carshes
- 不调用观察者 -> 没有崩溃
- 调用观察者但没有 LocalDate.format -> 没有崩溃
我得出的结论是问题出在格式函数中,但我不明白为什么。错误没有指向那个方向。有任何想法吗?
活动代码
class UITestingActivity: FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_greenhouse)
val dialog = LabTimesDialogFragment()
dialog.show(supportFragmentManager, "LabTimes")
}
}
片段代码
class TimesDialogFragment : DialogFragment() {
companion object Companion {
private val TAG: String = "TimesDialog"
}
private val datesViewModel: TimesViewModel by activityViewModels()
private lateinit var datesViews: Map<LiveData<LocalDate>, TextInputEditText>
override fun onCreateDialog(savedInstanceState: Bundle?) : Dialog {
val viewsArray: Array<TextInputEditText>
return activity?.let {
val builder = AlertDialog.Builder(it)
val inflater = requireActivity().layoutInflater
val rootView: View = inflater.inflate(R.layout.dialog_filter_times, null)
builder.setView(rootView)
.setPositiveButton(R.string.feed) { dialog, id -> closeDialog() }
.setNegativeButton(R.string.cancel) { dialog, id -> getDialog()?.cancel() }
val dialog: AlertDialog = builder.create()
val fromSampling: TextInputEditText = rootView.findViewById(R.id.from_sampling) ?: throw IllegalStateException("Missing date view in LabTimesFilterDialog")
val toSampling: TextInputEditText = rootView.findViewById(R.id.to_sampling) ?: throw IllegalStateException("Missing date view in LabTimesFilterDialog")
val fromSending: TextInputEditText = rootView.findViewById(R.id.from_sending) ?: throw IllegalStateException("Missing date view in LabTimesFilterDialog")
val toSending: TextInputEditText = rootView.findViewById(R.id.to_sending) ?: throw IllegalStateException("Missing date view in LabTimesFilterDialog")
val fromReceiving: TextInputEditText = rootView.findViewById(R.id.from_receiving) ?: throw IllegalStateException("Missing date view in LabTimesFilterDialog")
val toReceiving: TextInputEditText = rootView.findViewById(R.id.to_receiving) ?: throw IllegalStateException("Missing date view in LabTimesFilterDialog")
datesViews = mapOf(datesViewModel.fromSampling to fromSampling,
datesViewModel.toSampling to toSampling,
datesViewModel.fromSending to fromSending,
datesViewModel.toSending to toSending,
datesViewModel.fromReceiving to fromReceiving,
datesViewModel.toReceiving to toReceiving
)
for ((liveData, textView) in datesViews) {
liveData.observe(this, createDateFieldObserver(textView))
textView.setOnClickListener { v ->
Log.d(TAG, "hello"+v.id)
}
}
return dialog
} ?: throw IllegalStateException("Activity cannot be null")
}
private fun closeDialog() {
// save dates to ViewModel
// closeDialog
TODO()
}
private fun createDateFieldObserver(tw: TextInputEditText): Observer<LocalDate> {
return Observer { date ->
Log.d(TAG, "obs"+tw.id)
tw.setText(date.format(DateTimeFormatter.ISO_DATE))
//tw.setText("hello")
}
}
}
视图模型
class TimesViewModel : ViewModel() {
val fromSampling: MutableLiveData<LocalDate> = MutableLiveData(LocalDate.now())
val toSampling: MutableLiveData<LocalDate> = MutableLiveData(LocalDate.now())
val fromSending: MutableLiveData<LocalDate> = MutableLiveData(LocalDate.now())
val toSending: MutableLiveData<LocalDate> = MutableLiveData(LocalDate.now())
val fromReceiving: MutableLiveData<LocalDate> = MutableLiveData(LocalDate.now())
val toReceiving: MutableLiveData<LocalDate> = MutableLiveData(LocalDate.now())
}
自从我为android编程以来已经有一段时间了。我在这里使用的所有东西对我来说都是新的,所以如果你在这个小代码中发现了一个反模式,我会很高兴知道。
肿瘤坏死因子
解决方案
原来是因为布局。视图中没有足够的空间来显示整个日期。我不明白为什么它会导致 indexoutofbound 但解决方案是简单地使视图“wrap_content”或更大
推荐阅读
- azure - 如何使用 Azure Cosmosdb SDK 创建无服务器 cosmosdb?
- python - 用理解创建的字典的深层副本不起作用
- python-3.x - 使用位置和关键字参数的顺序
- jquery - 如何从其他表中获取值并在数据表中显示
- redux - React-Redux useSelector 无法传递数据
- python - 在 Python 中的类之外定义类方法
- java - Java Web Start 拒绝自签名证书
- javascript - 'self' 脚本被阻止加载,但是当我使用 nonce 时它们可以工作吗?
- python - Python 长度不匹配
- javascript - 使用 JavaScript 制作添加到收藏夹按钮