Java 返回空值的位置值

Java 返回空值的位置值,java,android,Java,Android,我正在尝试获取用户的位置,我有这个方法 public Location getCurrentLocation() { LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locManager.getBestProvider(criteria, true)

我正在尝试获取用户的位置,我有这个方法

public Location getCurrentLocation() {
    LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locManager.getBestProvider(criteria, true);
    Location location = locManager.getLastKnownLocation(provider);
    return location; 
}
private void loadMapResources() {
     userLocation = getCurrentLocation();
     //DRAW CIRCLE HERE
     if(userRadius != 0){
         Circle radiusCircle = googleMap.addCircle(new CircleOptions()
        .center(new LatLng(userLocation.getLatitude(), userLocation.getLongitude()))
        .radius(userRadius)
        .strokeWidth(4)
        .strokeColor(Color.parseColor("#FF6699"))
        .fillColor(Color.parseColor("#66F3F3F3"))
      );
         //SHOW USER ICON AT CURRENT LOCATION
         BitmapDescriptor userIcon = BitmapDescriptorFactory.fromResource(R.drawable.ic_user);
         googleMap.addMarker(new MarkerOptions()
                .title("YOU ARE HERE")
                .icon(userIcon)
                .position(new LatLng(userLocation.getLatitude(), userLocation.getLongitude()))
                );
     }
}
我有另一种方法,在这种方法中,它将基于
位置
变量的纬度和经度使圆形对象居中

public Location getCurrentLocation() {
    LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locManager.getBestProvider(criteria, true);
    Location location = locManager.getLastKnownLocation(provider);
    return location; 
}
private void loadMapResources() {
     userLocation = getCurrentLocation();
     //DRAW CIRCLE HERE
     if(userRadius != 0){
         Circle radiusCircle = googleMap.addCircle(new CircleOptions()
        .center(new LatLng(userLocation.getLatitude(), userLocation.getLongitude()))
        .radius(userRadius)
        .strokeWidth(4)
        .strokeColor(Color.parseColor("#FF6699"))
        .fillColor(Color.parseColor("#66F3F3F3"))
      );
         //SHOW USER ICON AT CURRENT LOCATION
         BitmapDescriptor userIcon = BitmapDescriptorFactory.fromResource(R.drawable.ic_user);
         googleMap.addMarker(new MarkerOptions()
                .title("YOU ARE HERE")
                .icon(userIcon)
                .position(new LatLng(userLocation.getLatitude(), userLocation.getLongitude()))
                );
     }
}

现在,该代码以前在使用和不使用GPS的设备上都有效。有谁能告诉我为什么
userLocation
会抛出
NullPointerException

请记住,
getLastKnownLocation
有时会返回
null
(例如,如果提供程序当前已禁用或没有最后已知的位置),因此您应该始终检查此方法的结果:

Location location = locManager.getLastKnownLocation(provider);
if (location == null){
    //handle this case
    //return location with some default coordinates, for example
}
return location;

我以前遇到过这样的问题。它与调用Location Location=locationManager.getLastKnownLocation(provider);,它返回null,因为无论哪个服务实际获取位置,在您到达userLocation.getLatitude()时都没有时间找到用户的位置;如果为空,请使用调试视图并查看变量。请查看此示例文件。它有一个工作结构,用于何时调用位置管理器以及何时获取位置。感谢您的快速回复。如何将默认坐标分配给位置变量?@DanielD我认为类似的方法会起作用:
location location=new location(provider);位置。设置纬度(…);地点。经度(…)null
的情况,就不必担心它