我正在尝试使用此处的sdk添加照片标记,但它会引发空点错误,我不明白为什么它是空指针错误或代码错误?
目录图像:https://i.stack.imgur.com/c7J4v.jpg
private void loadMapScene() {
// Load a scene from the SDK to render the map with a map style.
mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new MapScene.LoadSceneCallback() {
@Override
public void onLoadScene(@Nullable MapScene.ErrorCode errorCode) {
if (errorCode == null) {
mapView.getCamera().setTarget(new GeoCoordinates(17.3850, 78.4867));
mapView.getCamera().setZoomLevel(14);
GeoCoordinates geoCoordinates= new GeoCoordinates(17.3850, 78.4867);
MapImage mapImage = MapImageFactory.fromResource(context.getResources(), R.drawable.here_car);
MapMarker mapMarker = new MapMarker(geoCoordinates);
mapMarker.addImage(mapImage, new MapMarkerImageStyle());
mapView.getMapScene().addMapMarker(mapMarker);
} else {
Log.d(TAG, "onLoadScene failed: " + errorCode.toString());
}
}
});
}
编辑1错误:
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
W/System.err: at com.example.death.MainActivity$7.onLoadScene(MainActivity.java:272)
at com.here.sdk.core.threading.RunnableImpl.run(Native Method)
at com.here.sdk.core.threading.MainThreadTaskRunner.a(Unknown Source:10)
at com.here.sdk.core.threading.MainThreadTaskRunner.lambda$93nD-I9pqERpDgqXkXMj7C0moag(Unknown Source:0)
at com.here.sdk.core.threading.-$$Lambda$MainThreadTaskRunner$93nD-I9pqERpDgqXkXMj7C0moag.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
似乎您错过了设置context
的位置。试试这个:
Context context = MainActivity.this;
Resources resources = context.getResources();
MapImage mapImage = MapImageFactory.fromResource(resources, R.drawable.here_car);
根据堆栈跟踪,您正在从您的主要活动中执行此操作。
尝试像这样添加图像作为标记:
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.her_car)
MarkerOptions markerOptions = new MarkerOptions().position(latLng)
.title("Current Location")
.snippet("")
.icon(icon);
mMarker = googleMap.addMarker(markerOptions);
希望这会有所帮助!