Poras Bhardwaj
Poras Bhardwaj

Reputation: 1123

How to use PlaceAutoCompleteFragment widget in a Fragment - Android?

I am using PlaceAutoCompleteFragment inside a Fragment. First time when Fragment(App fragment in which PlaceAutoCompleteFragment is placed) opens it works fine like a charm. But, then second time I hit button to open Fragment it crashes with below error. It works only a single time.

FATAL EXCEPTION:
android.view.InflateException: Binary XML file line #64: Error inflating class fragment

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

This is how i am using this:

SupportPlaceAutocompleteFragment placeAutocompleteFragment = (SupportPlaceAutocompleteFragment) getActivity().getSupportFragmentManager().
findFragmentById(R.id.place_autocomplete_fragment);

placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        System.out.println(place.getAddress());
    }

    @Override
    public void onError(Status status) {
    }
});

Error is on this line in onCreateDialog method, where layout is inflating:

View view = inflater.inflate(R.layout.add_geofence_layout, null);

XML:

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

Wondering! Why this code works only once?

DialogFragment class:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.add_geofence_layout, null);

    viewHolder = new ViewHolder();
    viewHolder.initializeView(view);
    viewHolder.placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            System.out.println(place.getAddress());
        }

        @Override
        public void onError(Status status) {

        }
    });
}

Note:- I am using a PlaceautoCompleteFragment inside a DialogFragment. If i use this inside an Activity it works fine.

Upvotes: 4

Views: 2555

Answers (4)

Julia Williams
Julia Williams

Reputation: 113

Don't add PlaceAutocompleteFragment directly to your fragment layout. you need to add PlaceAutocompleteFragment dynamically and remove it on onDestroyView().

layout.xml

        <LinearLayout
            android:id="@+id/placeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">
        </LinearLayout>

Add PlaceAutocompleteFragment

  PlaceAutocompleteFragment autocompleteFragment=new PlaceAutocompleteFragment();
    FragmentManager fragmentManager = getActivity().getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.placeLayout,autocompleteFragment);
    fragmentTransaction.commit();

Remove it onDestroy

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (getActivity() != null) {
        FragmentManager fragmentManager = getActivity().getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(autocompleteFragment);
        fragmentTransaction.commit();
    }

}

Upvotes: 1

Filippe Carvalho
Filippe Carvalho

Reputation: 770

Try to remove the fragment at onDestroyView()

@Override
public void onDestroyView() {
    super.onDestroyView();
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(mPlaceAutocompleteFragment);
    ft.commit();
}

Upvotes: 0

corlaez
corlaez

Reputation: 1423

You should give more context regarding the layout. The Exceptions states: "Duplicate id " are you sure you aren't using the same id twice?

Upvotes: 0

sanedroid
sanedroid

Reputation: 1036

Try this if this works:

View view;
public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
          if(view == null) {
            view = inflater.inflate(R.layout.add_geofence_layout, null);
           }
        viewHolder = new ViewHolder();
        viewHolder.initializeView(view);

        viewHolder.placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                System.out.println(place.getAddress());
            }

            @Override
            public void onError(Status status) {

            }
        });
}

Upvotes: 0

Related Questions