我有一个应该返回用户位置的代码,但是经度一直返回null,因此我的应用程序一直崩溃。我该如何预防?

//get the user longitude and latitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

Geocoder geocoder;  //return meaningful data from user location
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();

最佳答案

添加支票。

if (location != null) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

至于为什么它不返回值,您需要添加一些日志记录。
要获得更完整的解决方案,请尝试以下操作:
public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get destination from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                //  Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(
                            LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    // Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(
                                LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

在这个实现中,这是一个类的方法,我从我的活动中调用它。以及在不再需要资源后释放资源所需的代码。
例如:
/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(LocFinder.this);
    }
}

免责声明请注意,这只是所需代码的一部分,有助于实现定位服务。当不使用资源和使用定位服务时,需要释放资源,因为项目的所有其他方面都需要在整个应用程序的程序流中进行管理。
查看Getting the Last Known LocationAndroid Location API - TutorialAndroid - Location Based Services以获得更全面的位置服务实现。

关于java - 在Android中,LocationManager使用getLastKnownLocation返回空经度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37417777/

10-12 02:25