Android Kotlin中的类型与接口不匹配

Android Kotlin中的类型与接口不匹配,android,android-studio,kotlin,Android,Android Studio,Kotlin,我有一个接口的实现问题,我尝试了一些东西,但没有成功 我不知道还能尝试什么,所以我请求你的帮助,提前谢谢你 MyGPSUtils.kt class GPSUtils(context: Context) { private var context: Context private var mSettingsClient: SettingsClient private var mLocationSettingsRequest: LocationSettingsRequest

我有一个接口的实现问题,我尝试了一些东西,但没有成功

我不知道还能尝试什么,所以我请求你的帮助,提前谢谢你

MyGPSUtils.kt

class GPSUtils(context: Context) {

    private var context: Context
    private var mSettingsClient: SettingsClient
    private var mLocationSettingsRequest: LocationSettingsRequest
    private var locationManager: LocationManager
    private var locationRequest: LocationRequest

    init {
        this.context = context
        locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        mSettingsClient = LocationServices.getSettingsClient(context)
        locationRequest = LocationRequest.create()
        locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        locationRequest.interval = (10 * 1000).toLong()
        locationRequest.fastestInterval = (2 * 1000).toLong()
        val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
        mLocationSettingsRequest = builder.build()
        builder.setAlwaysShow(true) //this is the key ingredient
    }

    fun turnGPSOn(onGpsListener: OnGpsListener) {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if (onGpsListener != null) {
                onGpsListener!!.gpsStatus(true)
            }
        } else {
            mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
                .addOnSuccessListener(context as Activity) {
                    if (onGpsListener != null) {
                        onGpsListener!!.gpsStatus(true)
                    }
                }.addOnFailureListener(context as Activity) { e ->
                    val statusCode = (e as ApiException).statusCode
                    when (statusCode) {
                        LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
                            val rae = e as ResolvableApiException
                            rae.startResolutionForResult(context as Activity, 1001)
                        } catch (sie: IntentSender.SendIntentException) { Log.i(TAG, "PendingIntent unable to execute request.") }

                        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                            val errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."
                            Log.e(TAG, errorMessage)
                            Toast.makeText(context as Activity, errorMessage, Toast.LENGTH_LONG).show()
                        }
                    }
                }
        }
    }

    interface OnGpsListener {
        fun gpsStatus(isGPSEnable: Boolean)
    }

}
class AssigmentFragment : Fragment() {

    var isGPS: Boolean = false

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val rootView = inflater.inflate(R.layout.fragment_assigment, container, false)

        GPSUtils(activity!!).turnGPSOn { isGPSEnable ->
            isGPS = isGPSEnable
        }
    }
}
MyAssigmentFragment.kt

class GPSUtils(context: Context) {

    private var context: Context
    private var mSettingsClient: SettingsClient
    private var mLocationSettingsRequest: LocationSettingsRequest
    private var locationManager: LocationManager
    private var locationRequest: LocationRequest

    init {
        this.context = context
        locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        mSettingsClient = LocationServices.getSettingsClient(context)
        locationRequest = LocationRequest.create()
        locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        locationRequest.interval = (10 * 1000).toLong()
        locationRequest.fastestInterval = (2 * 1000).toLong()
        val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
        mLocationSettingsRequest = builder.build()
        builder.setAlwaysShow(true) //this is the key ingredient
    }

    fun turnGPSOn(onGpsListener: OnGpsListener) {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if (onGpsListener != null) {
                onGpsListener!!.gpsStatus(true)
            }
        } else {
            mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
                .addOnSuccessListener(context as Activity) {
                    if (onGpsListener != null) {
                        onGpsListener!!.gpsStatus(true)
                    }
                }.addOnFailureListener(context as Activity) { e ->
                    val statusCode = (e as ApiException).statusCode
                    when (statusCode) {
                        LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
                            val rae = e as ResolvableApiException
                            rae.startResolutionForResult(context as Activity, 1001)
                        } catch (sie: IntentSender.SendIntentException) { Log.i(TAG, "PendingIntent unable to execute request.") }

                        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                            val errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."
                            Log.e(TAG, errorMessage)
                            Toast.makeText(context as Activity, errorMessage, Toast.LENGTH_LONG).show()
                        }
                    }
                }
        }
    }

    interface OnGpsListener {
        fun gpsStatus(isGPSEnable: Boolean)
    }

}
class AssigmentFragment : Fragment() {

    var isGPS: Boolean = false

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val rootView = inflater.inflate(R.layout.fragment_assigment, container, false)

        GPSUtils(activity!!).turnGPSOn { isGPSEnable ->
            isGPS = isGPSEnable
        }
    }
}
我得到了这个错误:

我不知道还有什么好尝试的,欢迎有想法。 提前感谢您的帮助。

试试这个:

GPSUtils(activity!!).turnGPSOn(object: GPSUtils.OnGpsListener {
     override fun gpsStatus(isGPSEnable: Boolean) {
           // Code block ...
     }
})
试试这个:

GPSUtils(activity!!).turnGPSOn(object: GPSUtils.OnGpsListener {
     override fun gpsStatus(isGPSEnable: Boolean) {
           // Code block ...
     }
})

如果您真的想使用这种简短的符号,您可以用Java编写接口:

interface OnGpsListener {
   void gpsStatus(boolean isGPSEnable);
}
通过这种方式,您可以通过以下方式在kotlin代码中使用它:

GPSUtils(activity!!).turnGPSOn(OnGpsListener { isGPSEnable ->
    isGPS = isGPSEnable
})
问题在于SAM转换仅适用于java接口,本文将对此进行一些解释:


如果您要将java代码移植到kotlin,那么呈现java接口可能会很方便,如前所述。

如果您真的想使用这种简短的表示法,您可以用java编写接口:

interface OnGpsListener {
   void gpsStatus(boolean isGPSEnable);
}
通过这种方式,您可以通过以下方式在kotlin代码中使用它:

GPSUtils(activity!!).turnGPSOn(OnGpsListener { isGPSEnable ->
    isGPS = isGPSEnable
})
问题在于SAM转换仅适用于java接口,本文将对此进行一些解释:


如果您正在将java代码移植到kotlin,那么呈现java接口可能会很方便,正如所说。

谢谢!!Szilárd Gerlei谢谢你!!斯吉拉德·格雷