【发布时间】:2019-01-22 13:37:01
【问题描述】:
我在我的片段中使用 kotlin 视图绑定。在某些情况下,应用程序会因IllegalStateException 而崩溃并查看为 null,我在可运行程序中访问它,该可运行程序使用延迟为 1.5 秒的处理程序调用。
numberRunnable = Runnable {
if (mobileView.text.toString().isNotEmpty() && Utility.isMobileNumberValid(mobileView.text.toString())) {
presenter.getCustomerDetails(Utility.getServerAcceptableContactNumber(mobileView.text.toString()))
}
}
mobileView 为空
处理程序代码:
handler.postDelayed(numberRunnable, 1500)
我知道有一种可能性可以检查我的片段中是否有isAdded,但由于我无法复制错误,我不确定它是否是问题所在。
【问题讨论】:
-
如果您的片段处于尚未调用
onCreateView的状态 - 或者它已经调用了onDestroyView,则视图将始终为空。 -
从onViewCreated()调用
-
您是否在不同的片段布局中使用“mobileView” id?如果是这样,您应该检查导入列表中的 kotlin 绑定类(“kotlinx.android.synthetic....”)是否对应于片段布局。
标签: android kotlin android-view kotlin-android-extensions