gpt4 book ai didi

java - Mapbox 无法在 fragment 中工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:50 25 4
gpt4 key购买 nike

我试图在 fragment 中显示 map 框,但出现以下错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法“void com.mapbox.mapboxsdk.maps.MapView.onResume()”

这是 map fragment 类:

public class Map extends android.support.v4.app.Fragment {
private MapView mapView;

public Map() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_map, container, false);

MapView mapView = (MapView) layout.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {

// Set map style
mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);

// Set the camera's starting position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(41.885, -87.679)) // set the camera's center position
.zoom(12) // set the camera's zoom level
.tilt(20) // set the camera's tilt
.build();

// Move the camera to that position
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}


});
return layout;
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}

我是不是漏掉了什么?我正在关注此链接:https://www.mapbox.com/help/first-steps-android-sdk/

最佳答案

您有两个 map View 实例。一个是 oncreate 方法内部的局部变量,另一个是全局变量。

并且全局变量没有初始化,所以改变这一行

MapView mapView = (MapView) layout.findViewById(R.id.mapview);

进入这个

mapView = (MapView) layout.findViewById(R.id.mapview);

我想你明白这个问题了。

关于java - Mapbox 无法在 fragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com