如何在 Kotlin 中正确初始化地理围栏列表

How to properly initialize geofence list in Kotlin

我正在尝试让地理围栏在 Android Studio 上运行。调试时,我收到 "No geofence has been added to this request."

下面的代码适用于其他人。我也试过 lateinit var geofencelist : ArrrayList<Geofence> 但是错误消息 returns geofencelist 未正确初始化。

> lateinit var geofencelist : MutableList<Geofence>

已经尝试过 MutableList 和 lateinit 以及以下风格

> private var geofencelist : ArrayList<Geofence>? = null 
> 
> //geofence builder successful geofencelist?.add(geofence) 
> 
> Log.e("D", geofencelist?.count().toString())

收到的输出:

E/D: null Caused by: java.lang.IllegalArgumentException: No geofence has been added to this request.

蒂亚

如果您没有将 geofencelist 设置为非空值,

geofencelist?.add(geofence) 将不执行任何操作,这就是 point of ?.

显然你不应该做一些棘手的事情,因为列表初始化是微不足道的;刚刚

val geofencelist = mutableListOf<Geofence>

然后 geofencelist.add(geofence) 你有 ?.add