我在阅读文档,上面写着:
When using the API in fully interactive mode, users of the MapView class must forward the following activity lifecycle methods to the corresponding methods in the MapView class: onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), onSaveInstanceState(), and onLowMemory().
我设法在片段的onCreate()方法中实现了除mapView.onCreate()之外的所有生命周期方法。当我实现此功能时,应用程序崩溃:
private lateinit var mapView: MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mapView = view.findViewById(R.id.mapView) as MapView
mapView.onCreate(savedInstanceState)
}
据我了解,这是因为 View 尚未在onCreate()中初始化,这就是为什么findViewById无法正常工作的原因。文档有问题吗?
最佳答案
您应该改为在Fragment的mapView
中初始化onCreateView
,如下所示(Java):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_map_page, container, false);
mMapView = (MapView) v.findViewById(R.id.map_view);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this); //this is important
return v;
}
关于android - fragment 中的mapView.onCreate(savedInstanceState),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58899681/