gpt4 book ai didi

android - 类转换异常 : ApiException cannot be cast to RevolvableApiException after updating location library to 21 version

转载 作者:行者123 更新时间:2023-12-05 03:17:18 24 4
gpt4 key购买 nike

我已将我的应用程序中的位置服务库更新到最新的 21 版本:
com.google.android.gms:play-services-location:21.0.0
它打破了在用户手机上启用位置设置的逻辑。

我找到了包含有关此过程的文档的更新页面: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient ,
并使用下面的代码触发弹出窗口,该弹出窗口应要求用户允许在手机上启用位置访问:

val locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.setMinUpdateIntervalMillis(5000).build()

val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)

val client: SettingsClient = LocationServices.getSettingsClient(activity)
val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
task.addOnCompleteListener {
try {
task.getResult(ApiException::class.java)
} catch (exception: ApiException) {
when (exception.statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
try {
val resolvable = exception as ResolvableApiException
resolvable.startResolutionForResult(
activity,
1
)
} catch (e: Exception) {
e.printStackTrace()
}
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {

}
}
}
}

但是文档中的代码在这一行抛出 ClassCastException:
val resolvable = exception as ResolvableApiException,
无法弄清楚如何处理最新的位置更新,在此先感谢您的帮助。

最佳答案

错误:异常 e 被实例化为“ApiException”,因此无法转换为 ResolveApiException(子类)。

e.getClass();//returnsString ApiException instead of ResolveApiException

可能的解决方法:使用 ResolvableApiException 构造函数,仅需要状态代码。

ResolvableApiException resolvable = new ResolvableApiException(exception.getStatus());
//use ResolvableApiException Constructor
//public ResolvableApiException (Status status)

关于android - 类转换异常 : ApiException cannot be cast to RevolvableApiException after updating location library to 21 version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74183743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com