0

I'm trying to use Google's Place Autocomplete (https://developers.google.com/places/android-api/autocomplete) to implement into a fragment in my app. But I got this error when navigating from this fragment to other fragment and then coming back into this fragment

 Caused by: java.lang.IllegalArgumentException: Binary XML file line #30: Duplicate id 0x7f0f010f, tag null, or parent id 0x7f0f00c0 with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
                  at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2148)

This is the java code

private PlaceAutocompleteFragment mSearchPAF;

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

    View view = inflater.inflate(R.layout.some_layout, container, false);
    mSearchLocationPAF = (PlaceAutocompleteFragment) parentActivity.getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
}

This is the XML file

<RelativeLayout
    android:id="@+id/someRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/some_drawable">
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

I also have another map fragment in this same java and xml file but I managed to solve it by following the answers in this post (Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment) but I can't find any solution for this Place Autocomplete fragment

Rushabh Shah
  • 371
  • 1
  • 7
  • 22
the newbie coder
  • 652
  • 2
  • 8
  • 27

2 Answers2

3

I have solved this problem myself by following one of the answers here Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

private PlaceAutocompleteFragment mSearchPAF;
@Override
public void onDestroyView() {
    super.onDestroyView();
    PlaceAutocompleteFragment f = (PlaceAutocompleteFragment) getFragmentManager()
                                     .findFragmentById(R.id.place_autocomplete_fragment);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}
the newbie coder
  • 652
  • 2
  • 8
  • 27
1

I am currently working on my Project and implemented my main activity with multiple fragments(Still a novice in Android Studio). In my Navigation fragment, I used an googleAutocomplete provided by Google.

This code here is implemented inside the OnCreateView().

mPlace_Starting = (PlaceAutocompleteFragment)
        this.getChildFragmentManager().findFragmentById(R.id.place_starting);

AutocompleteFilter countryFilter = new AutocompleteFilter.Builder()
        .setTypeFilter(Place.TYPE_COUNTRY)
        .setCountry("PH")
        .build();

mPlace_Starting.setFilter(countryFilter);

mPlace_Starting.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place:
        Log.i(TAG, "Starting Place:" + place.getName());
        Toast.makeText(getActivity(), "Starting Place: " + place.getName(),
                Toast.LENGTH_SHORT).show();

        double mLongitude = place.getLatLng().longitude;
        double mLatitude = place.getLatLng().latitude;

        mStartingpoint = new Waypoint(mLongitude,mLatitude);
        mStartpoint = new GeoCoordinate(mLongitude,mLatitude);
    }
    @Override
    public void onError(Status status) {
        //TODO: Handle the Error.
        Log.i(TAG, "An error occured: " + status);
    }
});

I inflated my PlaceAutocompleteFragment widget from Google by using getChildFragmentManager() since the widget is a fragment to be inflated inside another fragment(my Navigation Fragment).