【发布时间】:2020-01-08 05:10:00
【问题描述】:
我从 NETWORK_PROVIDER 获取位置如下:
if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
val locationManager = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
// Define a listener that responds to location updates
val locationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
// Called when a new location is found by the network location provider.
if (location != null) {
lastLocation = location
currentLatLng = LatLng(location.latitude, location.longitude)
if (currentLatLng != null &&
currentLatLng.latitude != null &&
currentLatLng.longitude != null
) {
sharedPreferenceManager?.setStringData(
Constant.PrefKey.userLatitude,
"" + currentLatLng.latitude
)
sharedPreferenceManager?.setStringData(
Constant.PrefKey.userLongitude,
"" + currentLatLng.longitude
)
getAddress(currentLatLng)
}
}else{
ToastUtil.displayLongDurationToast(mContext,"Could not get Location. Please turn on GPS and try again.")
getAndSaveCurrentLocation()
}
}
}
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f,locationListener)
但是,在方法 requestLocationUpdates 的最后一行,我得到的错误是:
以下函数均不能使用参数调用 提供
可能是什么问题? 我已经传递了有效的参数。
编辑: 获取此行参数的错误:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f,locationListener)
【问题讨论】:
-
使用调试点并尝试使用它。可能是其中一个条件是错误的,没有下一个。
-
@PranavPatel 最后一行的参数问题。已编辑请检查
-
你能在这里添加一个错误吗?我猜它应该是 nullPointerException 的监听器。
标签: android kotlin location locationmanager