I face this exception in my app in this line in onDestroyView
:
fm.beginTransaction().remove(mapFragment).commit();
My code is:
In onCreateView
of Fragment
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
googleMap = mapFragment.getMap();
// Retrieve the PlaceAutocompleteFragment.
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
// Setting a click event handler for the videosHashMap
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
serviceReqLat = latLng.latitude;
serviceReqLng = latLng.latitude;
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(getActivity(), Locale.getDefault());
try {
addresses = geocoder.getFromLocation(serviceReqLat, serviceReqLng, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
serviceReqAddress = addresses.get(0).getAddressLine(0);
// Clears the previously touched position
googleMap.clear();
// Animating to the touched position
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
googleMap.addMarker(markerOptions);
}
});
// Register a listener to receive callbacks when a place has been selected or an error has
// occurred.
autocompleteFragment.setOnPlaceSelectedListener(this);
In onDestroyView
@Override
public void onDestroyView() {
FragmentManager fm = getFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null || mapFragment.isResumed()) {
fm.beginTransaction().remove(mapFragment).commit();
}
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
if (autocompleteFragment != null || autocompleteFragment.isResumed()) {
getActivity().getFragmentManager().beginTransaction().remove(autocompleteFragment).commit();
}
super.onDestroyView();
}
I tried may solution but still got this exception, how solve this issue? Thanks in advance.