我正在编写一个Android应用程序。
在我的应用中,我是在MainActivity中分派ACTION_IMAGE_CAPTURE意向后立即调用地图片段。
地图片段片段为:
public class MapFragment extends Fragment implements OnMapReadyCallback {
.
.
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_map, container, false);
return mView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = mView.findViewById(R.id.map);
if (mMapView != null) {
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
.
.
.
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getActivity());
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
如果我不调度ACTION_IMAGE_CAPTURE意图,则该片段按计划工作,但是,当调度该意图时,MapsInitializer.initialize(getActivity())中的上下文为空
我认为我需要更好地同步ACTION_IMAGE_CAPTURE意图完成和onMapReady方法,但不确定如何做到。
任何帮助将不胜感激。
谢谢
答案 0 :(得分:0)
更改为此,
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = mView.findViewById(R.id.map);
//initialize before calling map onCreate
MapsInitializer.initialize(getActivity());
if (mMapView != null) {
//instead of passing null its a good practice to use savedInstanceState
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}