app getting crash while resume app

1.6k views Asked by At

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.

2

There are 2 answers

0
Rajesh On

You need to remove fragment before push new one in your fragment view have been re-created so add ==null condition in onCreatedView method,

View mainView;// this should be global variable.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(mainview==null){
           mainView = inflater.inflate(R.layout.fragment_layout, container, false);
           initializeView();//initialize all views in this method instead of onActivityCreated 
        }
        return mainView;
    }

Add this when you push above fragment to avoid duplication

try {
     android.app.Fragment fragment = getFragmentManager().findFragmentById(R.id.map_online);
     if (fragment != null) {
         android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
         ft.remove(fragment);
         ft.commit();
     }
} catch (Exception e) {
  e.printStackTrace();
}
0
Rishabh Mahatha On

Your app getting crash because static fragment inside fragment can not be remove or replace , you can only hide or show this static fragment. make your xml file like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coreLayout"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical">

        <FrameLayout
            android:id="@+id/fragment_home_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</LinearLayout>

in your fragment.java file write this code:

 PlaceAutocompleteFragment autocompleteFragmentSourceAddress = new PlaceAutocompleteFragment();
  replaceFragment(autocompleteFragmentSourceAddress, R.id.fragment_home_container);