Please any one help me, I am new in android studio, my app getting crash while resume application,
I have three different fragment layout which have
<fragment
android:id="@+id/map_online"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/action_bar" /> with different ID.
while resume application I need to load one Fragment with map,
I am getting following error:
Caused by: java.lang.IllegalArgumentException: Binary XML file line
39: Duplicate id 0x7f0f018a, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
Please any one sugggest how to solve this.
This is my fragment class:
public class ClientRequestFragment extends Fragment implements OnMapReadyCallback {
View clientRequestView;
private static final String TAG = ClientRequestFragment.class.getSimpleName();
GoogleMap googleMap;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_online);
mapFragment.getMapAsync(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater li = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
clientRequestView = li.inflate(R.layout.offline_fragment, container, false);
return clientRequestView;
}
public void addFragment(Fragment fragment, boolean addToBackStack,
String tag, boolean isAnimate) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (isAnimate) {
ft.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
}
if (addToBackStack) {
ft.addToBackStack(tag);
}
ft.replace(R.id.content_frame, fragment, tag);
ft.commitAllowingStateLoss();
}
@Override
public void onClick(View view) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
//googleMap.setMyLocationEnabled(true);
/* try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
getActivity(), R.raw.retro_style_json));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}*/
}
@Override
public void onDestroyView() {
super.onDestroyView();
FragmentManager fm = getActivity().getSupportFragmentManager();
Fragment fragment = (fm.findFragmentById(R.id.map_online));
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
@Override
public void onPause() {
super.onPause();
}
}
Thank you.
You need to remove fragment before push new one in your fragment view have been re-created so add
==null
condition inonCreatedView
method,