Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LocationManager在Android中使用getLastKnownLocation返回空经度_Java_Android_Android Location - Fatal编程技术网

Java LocationManager在Android中使用getLastKnownLocation返回空经度

Java LocationManager在Android中使用getLastKnownLocation返回空经度,java,android,android-location,Java,Android,Android Location,我有一个应该返回用户位置的代码,但是经度一直返回null,结果我的应用程序一直崩溃。我如何防止这种情况 //get the user longitude and latitude LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); do

我有一个应该返回用户位置的代码,但是经度一直返回
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();
//获取用户的经度和纬度
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
Location Location=lm.getLastKnownLocation(LocationManager.GPS\U提供程序);
double longitude=location.getLongitude();
双纬度=location.getLatitude();
地理编码器//从用户位置返回有意义的数据
列出地址;
geocoder=新的geocoder(这个,Locale.getDefault());
地址=地理编码器.getFromLocation(纬度,经度,1);
地址=地址.get(0).getAddressLine(0);
city=addresses.get(0.getLocation();
添加支票

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);
    }
}
免责声明请注意,这只是所需代码的一部分,用于帮助实施定位服务。资源在未使用时需要释放,位置服务的使用(与项目的所有其他方面一样)需要在整个应用程序的程序流中进行管理


查看或,以获得更全面的位置服务实现。

请在double-longitude=location.getLongitude()上发布错误stacktraceNull指针异常;您的
location
为空,而不是您的经度。
lm.getLastKnownLocation
可能返回空。如果此位置返回空,我如何处理此位置?我在此行中出错locationManager=(locationManager)MContext我在这里使用什么值MIN\u TIME\u BW\u UPDATES,MIN\u DISTANCE\u CHANGE\u FOR\u UPDATES,如果不实现
onLocationChanged()
方法来接收位置更新,那么调用
requestLocationUpdates()
有什么意义?GPS在更新位置之前至少会有几秒钟的延迟,因此在这之后立即调用
getLastKnownLocation()
将不会为您提供最新的位置。不仅如此,位置更新也不会被取消,因此GPS将保持活动状态,无论您稍后是否再次调用此方法,它都会耗尽电池电量。这不是获取最新位置的良好实现,不应进行升级。