Reputation: 411
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().
I managed to implement every lifecycle methods except mapView.onCreate() in the fragment's onCreate() method. 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. Is this something wrong with the documentation?
Upvotes: 1
Views: 1086
Reputation: 294
You should instead initialize your mapView
in Fragment's onCreateView
like this (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;
}
Upvotes: 1