0

I am trying to implement PlaceAutoCompleteFragment inside a fragment. I have end up with this from official documentation):

public class SecondFragment extends Fragment
        implements OnMapReadyCallback {

      PlaceAutocompleteFragment placeAutoComplete;
      .....

      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_second, null, false);
    
    
        // Create Map
        if (savedInstanceState != null) {
            mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
            latlang.Lat = mLastKnownLocation.getLatitude();
            latlang.Lang = mLastKnownLocation.getLongitude();
        }
    
        // Construct a FusedLocationProviderClient.
        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
        getLocationPermission();
        getDeviceLocation();
    
        placeAutoComplete = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete);
        placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
    
                    Log.d("Maps", "Place selected: " + place.getName());
                }
    
                @Override
                public void onError(Status status) {
                    Log.d("Maps", "An error occurred: " + status);
                }
            });
    
    
        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
        //Search Place
    
    
        return view;
      }

and the corresponding layout is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondFragment"
    android:orientation="vertical"
    android:weightSum="1">

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

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

So, I got a searchbar, but the application is crashing with error:

Process: com.example.phocast, PID: 6141
    android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
    Caused by: java.lang.IllegalArgumentException: Binary XML file line #0: Duplicate id 0x7f0a0094, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
      at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3541)
      at android.app.FragmentController.onCreateView(FragmentController.java:98)
      at android.app.Activity.onCreateView(Activity.java:6219)
      at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
      at com.example.phocast.SecondFragment.onCreateView(SecondFragment.java:76)

The line 76 of the SecondFragment.java is simply the layout inflator line of onCreateView:

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

One point is the documentation asks to put the above code inside an activity, but I have put it inside a fragment and used getActivity().getFragmentManager()!

May this lead to the error?

Community
  • 1
  • 1
BaRud
  • 3,055
  • 7
  • 41
  • 89
  • sorry but I don't how these two question is related. Can you please explain? – BaRud Feb 26 '18 at 19:05
  • >You shouldn't be inflating fragments inside other fragments https://stackoverflow.com/a/19815266/436938 – Sergey Glotov Feb 26 '18 at 19:12
  • You are trying to use nested fragments from xml which is still not supported AFAIK. You need to add the autocomplete and map fragments dynamically from code in your "SecondFragment" .More info here https://developer.android.com/about/versions/android-4.2.html#NestedFragments – Alex Feb 26 '18 at 19:13
  • Ah...now I can see the relation. But the answer has a "don't try this at home" tag. So, given my map is inside a fragment, is there any way I can add a place autocomplete? say, from toolbar? – BaRud Feb 26 '18 at 19:29

0 Answers0