Binary XML file line #26: Duplicate id, tag null, or parent id with another fragment

3

I'm trying to insert a fragment to another and I’ve succeed to do this until I’ve lunch the main fragment for the first time it's working but when I’m trying to reload the fragment the app crash, and i have this error:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #26:
Duplicate id 0x7f0e00e2, tag null, or parent id 0xffffffff with another
fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

This is my fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View  rootView = inflater.inflate(R.layout.source_destination,container, false);
PlaceAutocompleteFragment sourcr_autocompleteFragment = (PlaceAutocompleteFragment)
          getActivity().getFragmentManager()
.findFragmentById(R.id.sourcr_autocomplete_fragment);
return rootView;

// List item

}

This is my XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="10dp"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_medium"
            android:layout_marginBottom="@dimen/margin_medium">

            <fragment
                android:id="@+id/sourcr_autocomplete_fragment"    
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"           
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />

        </android.support.v7.widget.CardView>
    </LinearLayout>
android
android-fragments
android-nested-fragment
asked on Stack Overflow Jun 22, 2016 by Rgv • edited Jun 22, 2016 by Arjun saini

2 Answers

4

I just ran into the same problem and could solve it by removing the fragment in the onDismiss callback.

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);

    if (getActivity() != null) {
        FragmentManager fragmentManager = getActivity().getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }
}
answered on Stack Overflow Aug 18, 2016 by unique entity • edited Aug 18, 2016 by unique entity
3

overide the onDestroyView method in the fragment

public void onDestroyView() {
    super.onDestroyView();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.remove(fragment);
    fragmentTransaction.commit();
}

kotlin:

override fun onDestroyView() {
    super.onDestroyView()
    activity?.supportFragmentManager?.also { fragmentManager ->
        fragmentManager.findFragmentById(R.id.aboutFragment)?.also { fragment ->
            fragmentManager.beginTransaction().remove(fragment).commit()
        }
    }
}
answered on Stack Overflow Jun 22, 2016 by Manish Jain • edited Oct 1, 2018 by Bernardo Ferrari

User contributions licensed under CC BY-SA 3.0