我遇到了使用MapBox与GPS使用相关的问题。每当我在打开它之后尝试使用这些方法来使用GPS时,我都会收到此错误(这次我尝试使用getLatitude):
java.lang.NullPointerException: Attempt to invoke virtual method
'double com.mapbox.services.commons.models.Position.getLatitude()' on a null object reference
我确实认为GPS还没有找到位置,这就是为什么我得到一个空引用,因为它确实有效,并且如果我在访问使用该功能的函数之前等待一段时间,则会显示正确的位置GPS方法。我在这里找到了与该错误相关的内容,但这是使用Google Maps。
由于我解决了这个问题,这不是与GPS关闭相关的问题,但我无法找到一种方法或方法来了解GPS何时找到一个位置,以及所以我可以在向用户显示其位置之前创建一个加载屏幕。
以下是我收到错误的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final MapboxMap mapboxMap) {
map = mapboxMap;
//...
// Button that shows the user their location
floatingActionButton = (ImageButton) findViewById(R.id.location_toggle_fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Verifica se o usuario esta com o GPS ligado
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (map != null) {
toggleGps(!map.isMyLocationEnabled());
}
}else{
//function that warns the user that they don't have their GPS turned on
montaAlertaSemGps();
}
}
});
//InfoWindow Data
mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
@Nullable
@Override
public View getInfoWindow(@NonNull Marker marker) {
//...
final Marker marcador = marker;
if (!marker.getTitle().equals("Origem")&&!marker.getTitle().equals("Destino")) {
botaoIr = (Button) v.findViewById(R.id.botaoIr);
botaoIr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Checks if the user have their GPS turned on
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
origin = Position.fromCoordinates(locationServices.getLastLocation().getLongitude(),locationServices.getLastLocation().getLatitude());
LatLng ponto = marcador.getPosition();
destination = Position.fromCoordinates(ponto.getLongitude(), ponto.getLatitude());
//Error starts below
origem = new MarkerOptions()
.position(new LatLng(origin.getLatitude(), origin.getLongitude()))
.title("Origem")
.icon(iconeRota);
destino = new MarkerOptions()
.position(new LatLng(destination.getLatitude(), destination.getLongitude()))
.title("Destino")
.icon(iconeRota);
//...
}
} else {
//...
}
}
});
}
return v;
}
});
}
});
}
private void toggleGps(boolean enableGps) {
if (enableGps) {
// Checa se o usuario permitiu acesso a localizacao
if (!locationServices.areLocationPermissionsGranted()) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_LOCATION);
} else {
enableLocation(true);
}
} else {
enableLocation(false);
}
}
private void enableLocation(boolean enabled) {
if (enabled) {
Location lastLocation = locationServices.getLastLocation();
if (lastLocation != null) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastLocation), 16));
origin = Position.fromCoordinates(locationServices.getLastLocation().getLongitude(),locationServices.getLastLocation().getLatitude());
}
locationServices.addLocationListener(new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location), 16));
locationServices.removeLocationListener(this);
}
}
});
floatingActionButton.setImageResource(R.drawable.ic_location_disabled_black_24dp);
} else {
floatingActionButton.setImageResource(R.drawable.ic_my_location_24dp);
}
map.setMyLocationEnabled(enabled);
}
我无法在库中找到可以为我做这样的事情的方法。
编辑:我发现了问题,但我找不到解决办法。问题出在" enableLocation"这是" lastLocation":
Location lastLocation = locationServices.getLastLocation();
if (lastLocation != null) {
//...
}
在我开启GPS之后,该变量完全是NULL,因为它还没有带位置,这就是为什么函数“getLatitude”'尝试在NULL对象上调用方法。我已经使用' else'来调试代码。如果条件如上,但我不确定我应该在'其他'条件,或在lastLocation归属之前。
答案 0 :(得分:0)
知道您是否有位置的方法是调用LocationManager.requestLocationUpdates。当位置存在时,它将调用您的回调。当发生这种情况时,您知道位置很好。
答案 1 :(得分:0)
不幸的是,这不是mapbox库中的常见错误。我已经和开发人员谈过,这不应该发生,因为我的手机可能会出错。
现在我将关闭它,如果我找到解决方案,我会为每个人编辑这篇文章。