本文介绍了更改maptype时,mapbox.getStyle()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将mapStyle从Style.MAPBOX_STREETS更改为Style.SATELLITE_STREETS(反之亦然)时,mapbox出现问题. (其他样式也会发生这种情况,这些只是示例)

I have a problem with mapbox when changing the mapStyle from Style.MAPBOX_STREETS to Style.SATELLITE_STREETS (and vice versa) quickly in a row. (This happens with other styles too, those are just examples)

我的代码:

public void btnChangeMapType(View view) {
    this.mapboxMap.getStyle(style -> {
        String styleUri = this.mapboxMap.getStyle().getUri();
        if (this.animator != null)
            this.animator.cancel();

        if (styleUri.equalsIgnoreCase(SATELLITE_STREETS)) {
            changeMapType(Style.MAPBOX_STREETS, this.mapboxMap.getStyle());
        } else {
            changeMapType(Style.SATELLITE_STREETS, this.mapboxMap.getStyle());
        }
    });
}
private void changeMapType(String mapboxMapType, Style mapboxStyle) {
    this.mapboxMap.setStyle(mapboxMapType, style -> {
        if (this.previousLocation != null) {
            this.positionGeoJson = new GeoJsonSource(MAP_LAYER_SOURCE_ID, Feature.fromGeometry(Point.fromLngLat(this.previousLocation.getLongitude(), this.previousLocation.getLatitude())));
        } else {
            this.positionGeoJson = getNewGeoJsonPosition();
        }

        if (this.hasFocus && this.animator != null) {
            drawDrivenLine();
            this.animator.start();
        }
    });
}

如果我多次单击快速调用 btnChangeMapType 的按钮,则会在行 String styleUri = this.mapboxMap.getStyle上出现错误().getUri(); 在btnChangeMapType()

And if i click on a Button that calls btnChangeMapType quickly multiple times i get an error on Line String styleUri = this.mapboxMap.getStyle().getUri(); in btnChangeMapType()

错误日志:

2020-02-20 11:31:50.828 14916-14916/at.myprojects.project E/Mbgl-MapChangeReceiver: Exception in onDidFinishLoadingStyle
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mapbox.mapboxsdk.maps.Style.getUri()' on a null object reference

有没有办法防止我的应用崩溃或出现此错误?

Is there a way too keep my app from crashing/having this error?

推荐答案

String styleUri = this.mapboxMap.getStyle().getUri();中,不需要getStyle(),因为您已经在getStyle()块中,该块返回该行上方的Style对象(style -> {).因此它应该是String styleUri = style.getUri();.我认为这将解决NullPointerException消息.

In String styleUri = this.mapboxMap.getStyle().getUri(); , the getStyle() is not needed because you're already in the getStyle() block that returns a Style object above that line (the style -> {). So it'd be String styleUri = style.getUri(); instead. I think this will solve the NullPointerException message.

此外,您似乎在任何地方都没有使用Style mapboxStyle参数,因此您可以从changeMapType()中删除该参数.

Also, it doesn't look like you're using the Style mapboxStyle parameter anywhere, so you can remove that from changeMapType().

这篇关于更改maptype时,mapbox.getStyle()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 19:52