Kotlin - requestLocationUpdates() 方法的方法参数问题
Kotlin - Issue in Method Parameters for requestLocationUpdates() method
我正在从 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 的最后一行出现错误:
None of the following function can be called with the arguments
supplied
可能是什么问题?
我已经传递了有效参数。
编辑:
在此行获取参数错误:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f,locationListener)
很可能您导入了错误的 LocationListener。
您可能正在使用 com.google.android.gms.location.LocationListener
但是你需要导入android.location.LocationListener
检查您的导入并更改导入以更正 LocationListener 并且调用应该有效。
我正在从 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 的最后一行出现错误:
None of the following function can be called with the arguments supplied
可能是什么问题? 我已经传递了有效参数。
编辑: 在此行获取参数错误:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f,locationListener)
很可能您导入了错误的 LocationListener。 您可能正在使用 com.google.android.gms.location.LocationListener
但是你需要导入android.location.LocationListener
检查您的导入并更改导入以更正 LocationListener 并且调用应该有效。