Fused Location Provider 方法 getLastLocation() 返回空值

问题描述 投票:0回答:0

我正在实施一个位置 android 应用程序,使用融合位置提供程序客户端。它几天前一直在工作,但实际上它崩溃了,因为它返回值 null,这是我得到的错误消息:java.lang.NullPointerException:Attempt在空对象引用上调用虚拟方法“double android.location.Location.getLongitude()”。 我怎么能弄清楚? 这是我实现的代码:

fusedLocationProviderClient.getLastLocation().addOnSuccessListener(baseView, position -> {
        Log.d("TAG", "updateGPS: position elements are : " + "lon = " + position.getLongitude() + "lat = " + position.getLatitude() + "alt " + position.getAltitude() + "Acc= " + position.getAccuracy() + " Speed = " + position.getSpeed());
        if (position.getAccuracy() <=20f) {
            location.setLongitude(position.getLongitude());
            location.setLatitude(position.getLatitude());
            location.setAltitude(position.getAltitude());
            location.setAccuracy(position.getAccuracy());
            location.setSpeed(position.getSpeed());
            
        }
       
java android-location fusedlocationproviderapi android-fusedlocation
© www.soinside.com 2019 - 2024. All rights reserved.