【问题标题】:Kotlin - Issue in Method Parameters for requestLocationUpdates() methodKotlin - requestLocationUpdates() 方法的方法参数中的问题
【发布时间】: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


【解决方案1】:

很可能您导入了错误的 LocationListener。 您可能正在使用 com.google.android.gms.location.LocationListener

但您需要导入 android.location.LocationListener

检查您的导入并将导入更改为正确的 LocationListener 并且调用应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2015-12-26
    • 2016-11-20
    • 1970-01-01
    相关资源
    最近更新 更多