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);位置。设置纬度(…);地点。经度(…)谢谢你,我的朋友:)你让我意识到优雅失败的重要性。根据你的经验,在覆盖范围好的地区使用GPS的设备会抛出这种空指针吗?@DanielD不,这并不常见。无论如何,如果您处理null
的情况,就不必担心它