简体   繁体   English

FusedLocationProviderClient 因 java.lang.NullPointerException 崩溃:指定为非空的参数位置为 null

[英]FusedLocationProviderClient crash with java.lang.NullPointerException: parameter location specified as non-null is null

I'm using FusedLocationProviderClient in my app and it works fine to get user current location but suddenly I got this crash我在我的应用程序中使用 FusedLocationProviderClient 并且它可以很好地获取用户当前位置但突然我遇到了这个崩溃

java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter location

here is how I define it inside my activity这是我在活动中定义它的方式

private lateinit var fusedLocationClient: FusedLocationProviderClient

// //

fusedLocationClient =
                    LocationServices.getFusedLocationProviderClient(this)

// the crash happens on the next code on (addOnSuccessListener) // 崩溃发生在 (addOnSuccessListener) 上的下一个代码上

fusedLocationClient.lastLocation
            .addOnSuccessListener { location ->
                    lat = location.latitude
                    lng = location.longitude
                    goToLocationOnMap(LatLng(lat, lng))
            }

The Task.OnSuccessListener is a Java class without paramenter-nullability annotation ( @NotNull or @Null ). Task.OnSuccessListener是一个Java class没有参数可空性注释( @NotNull@Null )。 Therefore Kotlin cannot figure out if the type is nullable or not-nullable and the compiler does not complain about unsafe usage of location (eg. location.latitude instead of location?.latitude ).因此 Kotlin 无法确定类型是可为空还是不可为空,并且编译器不会抱怨location的不安全使用(例如location.latitude而不是location?.latitude )。

There are 3 possible cases where the location can be null , according to the docs :根据文档,在 3 种可能的情况下,位置可以是null

  • Location is turned off in the device settings.位置在设备设置中已关闭。 The result could be null even if the last location was previously retrieved because disabling location also clears the cache.结果可能是 null 即使先前检索到最后一个位置,因为禁用位置也会清除缓存。
  • The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings.该设备从未记录其位置,这可能是新设备或已恢复出厂设置的设备的情况。
  • Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted.设备上的 Google Play 服务已重新启动,并且没有活动的 Fused Location Provider 客户端在服务重新启动后请求位置。

To sum up: Declare nullability explicitly and handle null-scenario .addOnSuccessListener { location: Location? -> /* null-safe code here */ }总结一下:明确声明可空性并处理空场景.addOnSuccessListener { location: Location? -> /* null-safe code here */ } .addOnSuccessListener { location: Location? -> /* null-safe code here */ }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:指定为非空的参数是 Java 代码中的 null 错误 - java.lang.IllegalArgumentException: Parameter specified as non-null is null error in Java code 错误:java.lang.IllegalArgumentException:指定为非null的参数为空Firebase事务Kotlin - Error: java.lang.IllegalArgumentException: Parameter specified as non-null is null firebase transaction kotlin 由java.lang.IllegalArgumentException引起的指定为非null的参数为null - Parameter specified as non-null is null caused by java.lang.IllegalArgumentException java.lang.IllegalArgumentException:指定为非空的参数是 null:- android,应用程序启动但立即崩溃 - java.lang.IllegalArgumentException: Parameter specified as non-null is null: - android, app starting but crashing straight away 指定为非空的参数是 null 和 BottomSheetDialog - Parameter specified as non-null is null with BottomSheetDialog java.lang.NullPointerException: inStream 参数是 null 在我的 Maven 项目中 - java.lang.NullPointerException: inStream parameter is null in my Maven Project java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull - java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull java.lang.NullPointerException:storage == null - java.lang.NullPointerException: storage == null 引起原因:java.lang.NullPointerException:lock == null - Caused by: java.lang.NullPointerException: lock == null 数组为null的java.lang.NullPointerException - Null with Array java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM