带有接口的 Kotlin 中的类型不匹配
Posted
技术标签:
【中文标题】带有接口的 Kotlin 中的类型不匹配【英文标题】:Type Mismatch in Kotlin with an interface 【发布时间】:2020-03-12 16:47:27 【问题描述】:我在实现接口时遇到问题,我尝试了一些方法但没有成功。
我不知道还能尝试什么,所以我请求你的帮助,提前谢谢你。
我的GPSUtils.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)
我的AssigmentFragment.kt
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
我得到了这个错误:
我不知道还有什么可以尝试的,欢迎提出想法。 提前感谢您的帮助。
【问题讨论】:
【参考方案1】:如果你真的想使用那个简短的符号,你可以用 Java 编写你的接口:
interface OnGpsListener
void gpsStatus(boolean isGPSEnable);
这样你就可以在你的 kotlin 代码中通过以下方式使用它:
GPSUtils(activity!!).turnGPSOn(OnGpsListener isGPSEnable ->
isGPS = isGPSEnable
)
问题在于 SAM 转换仅适用于 java 接口,本文对此进行了解释:
krossovochkin: kotlin java interop function references and sam conversions
如果您要将 java 代码移植到 kotlin,也许像 this SO answer 所说的那样,保留 java 接口很方便。
【讨论】:
【参考方案2】:试试这个:
GPSUtils(activity!!).turnGPSOn(object: GPSUtils.OnGpsListener
override fun gpsStatus(isGPSEnable: Boolean)
// Code block ...
)
【讨论】:
以上是关于带有接口的 Kotlin 中的类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章
在 Kotlin 中与 @RequestParam 一起使用时,自定义 HashMap 类获取参数类型不匹配
Android kotlin Object Any 类型不匹配