[英]mapView.onCreate(savedInstanceState) in fragment
I was reading the documentation and it says:我正在阅读文档,它说:
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().
在全交互模式下使用 API 时,MapView class 的用户必须将以下活动生命周期方法转发到 MapView class 中的相应方法:onCreate(), onPause(), onResume(), onDestroy()、onSaveInstanceState() 和 onLowMemory()。
I managed to implement every lifecycle methods except mapView.onCreate() in the fragment's onCreate() method.我设法在片段的 onCreate() 方法中实现了除 mapView.onCreate() 之外的所有生命周期方法。 The app crashes when I implement this:
当我实现这个应用程序崩溃:
private lateinit var mapView: MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mapView = view.findViewById(R.id.mapView) as MapView
mapView.onCreate(savedInstanceState)
}
From my understanding, it is because the view has not be initialized in onCreate() that's why findViewById would not work.据我了解,这是因为视图尚未在 onCreate() 中初始化,这就是 findViewById 不起作用的原因。 Is this something wrong with the documentation?
这是文档有问题吗?
You should instead initialize your mapView
in Fragment's onCreateView
like this (Java):您应该像这样(Java)在 Fragment 的
onCreateView
中初始化您的mapView
:
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.