Я только что закончил работу над картой, используя osm, но у меня есть такая ошибка
Я искал решение в интернете, но не нашел его
это коддля setupMap
// setting tile map
BingMapTileSource.setBingKey(getString(R.string.bingkey));
BingMapTileSource bingMap = new BingMapTileSource(null);
bingMap.setStyle(BingMapTileSource.IMAGERYSET_ROAD);
// menambah overlay ke dalam map
mapView.getOverlays().add(rotationGestureOverlay);
mapView.getOverlays().add(compassOverlay);
mapView.getOverlays().add(myLocationNewOverlay);
mapView.getOverlays().add(roadNodeMarkers);
//setting mapView
mapView.setTileSource(bingMap);
mapView.setTilesScaledToDpi(true);
mapView.setMinZoomLevel(1.0);
mapView.setMaxZoomLevel(21.0);
mapView.setMultiTouchControls(true);
mapView.setUseDataConnection(true);
mapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
mapController.animateTo(new GeoPoint(-5.402213, 105.264093));
}
и я получил ошибку вроде этого
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void org.osmdroid.api.IMapController.animateTo(org.osmdroid.api.IGeoPoint)' on a null object reference
at com.example.donordarahanjeng.directmap.setupMap(directmap.java:449)
at com.example.donordarahanjeng.directmap.onCreate(directmap.java:150)
, когда я нажму на ошибку, он направится к mapController.animateTo(new GeoPoint(-5.402213, 105.264093));
, и это мой полный код
public class directmap extends AppCompatActivity implements MapboxMap.OnMapClickListener
, PermissionsListener, View.OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback
, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
//, MapboxMap.OnMapClickListener, PermissionsListener, View.OnClickListener {
@BindView(R.id.mapViewosm)
MapView mapView;
@BindView(R.id.route)
FloatingActionButton route;
//private MapView mapView;
private MapboxMap mapboxMap;
private FusedLocationProviderClient locationProviderClient;
private PermissionsManager permissionsManager;
private LocationComponent locationComponent;
// variables for calculating and drawing a route
private DirectionsRoute currentRoute;
private static final String TAG = "DirectionsActivity";
private NavigationMapRoute navigationMapRoute;
private RoadManager roadManager;
private MyLocationNewOverlay myLocationNewOverlay;
private LocationManager manager;
private IMapController mapController;
private RotationGestureOverlay rotationGestureOverlay;
private GpsMyLocationProvider myLocationProvider;
private CompassOverlay compassOverlay;
protected FolderOverlay roadNodeMarkers;
//private MyLocationNewOverlay myLocationNewOverlay;
private GeoPoint startPoint, destPoint;
private Bitmap startIcon;
// private LocationHelper locationHelper;
private Location location;
private double lat, lng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getApplicationContext();
Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context));
//Mapbox.getInstance(directmap.this, getString(R.string.access_token));
setContentView(R.layout.activity_directmap);
ButterKnife.bind(this);
// mapView = (MapView) findViewById (R.id.mapViewosm);
// button = (Button) findViewById(R.id.startButton);
roadManager = new GraphHopperRoadManager(getString(R.string.apikey), true);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
route.setOnClickListener(this);
//mapView.onCre(savedInstanceState);
//mapView.getMapAsync(this);
startIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_mark);
//ambil lokasi pendonatur
lat = -5.402213; //getIntent().getDoubleExtra("latitude", 0);,
lng = 105.264113;//getIntent().getDoubleExtra("longitude", 0);
setupMap();
setupOverlay();
addMarker();
}
private void addMarker() {
Marker destMarker = new Marker(mapView);
destPoint = new GeoPoint(lat, lng);
destMarker.setPosition(destPoint);
destMarker.setTitle(getIntent().getStringExtra("title"));
destMarker.setSubDescription(getIntent().getStringExtra("username"));
destMarker.setIcon(getDrawable(R.drawable.ic_mark));
mapView.getOverlays().add(destMarker);
mapView.invalidate();
}
@Override
protected void onStop() {
super.onStop();
//mapView.onStop();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//mapView.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
//mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
// mapView.onLowMemory();
}
@Override
public void onClick(View v) {
/* if (v == button) {
boolean simulateRoute = true;
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.directionsRoute(currentRoute)
.shouldSimulateRoute(simulateRoute)
.build();
// Call this method with Context from within an Activity
NavigationLauncher.startNavigation(directmap.this, options);
startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
GeoPoint destination = destPoint;
ArrayList<GeoPoint> waypoints = new ArrayList<>();
waypoints.add(startPoint);
waypoints.add(destination);
// algoritma astar
roadManager.addRequestOption("algorithm=astarbi");
Road road = roadManager.getRoad(waypoints);
} */
if (v.getId() == R.id.route) {
getRoute2();
}
}
@SuppressLint("MissingPermission")
private void getRoute2() {
startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
GeoPoint destination = destPoint;
ArrayList<GeoPoint> waypoints = new ArrayList<>();
waypoints.add(startPoint);
waypoints.add(destination);
// algoritma astar
roadManager.addRequestOption("algorithm=astarbi");
Road road = roadManager.getRoad(waypoints);
// build rute overlay
Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
//mengatur warna dan lebar rute
roadOverlay.setColor(Color.BLUE);
roadOverlay.setWidth(7);
putRoadNodes(road);
mapView.getOverlays().add(roadOverlay);
mapView.invalidate();
}
private void setupOverlay() {
//setting map controller
mapController = mapView.getController();
mapController.setZoom(13.0);
//setting node marker
roadNodeMarkers = new FolderOverlay();
roadNodeMarkers.setName("Route Steps");
// setting compass
compassOverlay = new CompassOverlay(directmap.this
, new InternalCompassOrientationProvider(directmap.this), mapView);
compassOverlay.enableCompass();
compassOverlay.setPointerMode(true);
//setting provider lokasi
myLocationProvider = new GpsMyLocationProvider(this.getBaseContext());
myLocationProvider.setLocationUpdateMinTime(1000);
myLocationProvider.setLocationUpdateMinDistance(50);
//setting gesture rotasi
rotationGestureOverlay = new RotationGestureOverlay(mapView);
rotationGestureOverlay.setEnabled(true);
//setting location overlay
myLocationNewOverlay = new MyLocationNewOverlay(myLocationProvider, mapView);
myLocationNewOverlay.enableMyLocation();
myLocationNewOverlay.enableFollowLocation();
myLocationNewOverlay.setPersonIcon(startIcon);
}
private void setupMap() {
// setting tile map
BingMapTileSource.setBingKey(getString(R.string.bingkey));
BingMapTileSource bingMap = new BingMapTileSource(null);
bingMap.setStyle(BingMapTileSource.IMAGERYSET_ROAD);
// menambah overlay ke dalam map
mapView.getOverlays().add(rotationGestureOverlay);
mapView.getOverlays().add(compassOverlay);
mapView.getOverlays().add(myLocationNewOverlay);
mapView.getOverlays().add(roadNodeMarkers);
//setting mapView
mapView.setTileSource(bingMap);
mapView.setTilesScaledToDpi(true);
mapView.setMinZoomLevel(1.0);
mapView.setMaxZoomLevel(21.0);
mapView.setMultiTouchControls(true);
mapView.setUseDataConnection(true);
mapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
mapController.animateTo(new GeoPoint(-5.402213, 105.264093));
}
private void putRoadNodes(Road road) {
roadNodeMarkers.getItems().clear();
Drawable nodeIcon = getResources().getDrawable(R.drawable.ic_mark); //marker node
int n = road.mNodes.size();
MarkerInfoWindow infoWindow = new MarkerInfoWindow(R.layout.bonuspack_bubble, mapView);
TypedArray iconIds = getResources().obtainTypedArray(R.array.direction);
//ambil titik node
for (int i = 0; i < n; i++) {
RoadNode node = road.mNodes.get(i);
String instructions = (node.mInstructions == null ? "" : node.mInstructions);
Marker nodeMarker = new Marker(mapView);
nodeMarker.setTitle("Step " + (i + 1));
nodeMarker.setSnippet(instructions);
nodeMarker.setSubDescription(Road.getLengthDurationText(this, node.mLength, node.mDuration));
nodeMarker.setPosition(node.mLocation);
nodeMarker.setIcon(nodeIcon);
nodeMarker.setInfoWindow(infoWindow);
int iconId = iconIds.getResourceId(node.mManeuverType, R.drawable.ic_empty); //icempty
if (iconId != R.drawable.ic_empty) //icempty
{
Drawable image = ResourcesCompat.getDrawable(getResources(), iconId, null);
nodeMarker.setImage(image);
}
nodeMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);
roadNodeMarkers.add(nodeMarker);
}
iconIds.recycle();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
Что мне делать? спасибо