4

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.

Mike
  • 4,550
  • 4
  • 33
  • 47
Poras Bhardwaj
  • 1,073
  • 1
  • 15
  • 33
  • share the code you use to open the fragment – x0r May 19 '16 at 11:16
  • @R.Kirill Code i am using to open Fragment: AddGeoFenceFragment dialogFragment = new AddGeoFenceFragment(); dialogFragment.setAddGeoFenceListener(MainActivity.this); dialogFragment.setCancelable(false); dialogFragment.show(getSupportFragmentManager(), "AddGeoFenceFragment"); – Poras Bhardwaj May 19 '16 at 11:19
  • Have you looked at answers here : http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi . – WannaBeGeek May 19 '16 at 12:05
  • I guess you need to destroy/remove the places fragment in onDestroy of parent fragment – sanedroid May 20 '16 at 11:20
  • @PoojaGaikwad What do you mean by destroy/remove? Can you elaborate please. – Poras Bhardwaj May 20 '16 at 11:26
  • this happens when a layout is already in the context and you are trying to inflate it again.. Have you tried this? : View view; if(view == null) { view = inflater.inflate(R.layout.add_geofence_layout, null);} – sanedroid May 20 '16 at 11:33
  • @PoojaGaikwad I am getting this error on this line View view = inflater.inflate(R.layout.add_geofence_layout, null); when inflating my layout to view. Then how can i check view == null before inflating it? – Poras Bhardwaj May 20 '16 at 11:48
  • yeah if you add the null check.. the view will be inflated only when it is equal to null. as the error suggests you are trying to inflate the view that has already been inflated. Have you tried it?? – sanedroid May 20 '16 at 11:50
  • @PoojaGaikwad I have updated my question with the DialogFragment code. Please have a look. How can I get View view; there before inflating it? I must have to initialize it before check on it for null. – Poras Bhardwaj May 20 '16 at 11:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112503/discussion-between-poras-bhardwaj-and-pooja-gaikwad). – Poras Bhardwaj May 20 '16 at 12:03

4 Answers4

1

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();
    }

}

Julia Williams
  • 103
  • 1
  • 5
0

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) {

            }
        });
}
sanedroid
  • 1,036
  • 4
  • 16
  • 27
  • Tried, and it is not working dear. :) How can it be? We will get the view as null every time this class called, because view is not initialized yet. Right? – Poras Bhardwaj May 20 '16 at 12:12
0

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

corlaez
  • 1,352
  • 1
  • 15
  • 30
0

Try to remove the fragment at onDestroyView()

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