Adding PlaceAutocompleteFragment to fragment throws error

Amit Nair picture Amit Nair · May 3, 2016 · Viewed 13.6k times · Source

I have implemented PlaceAutocompleteFragment in activity and is working successfuly. But how to implement the same in fragment in android? I have implemented placeautocomplete fragment like this

 PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);

Error I am getting is

Incovertible types;cannot cast 'android.support.v4.app.Fragment' to com.google.android.gms.location.PlaceAutocompleteFragment '.

XML LAYOUT IS

  <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_background"
            card_view:cardCornerRadius="4dp"
            card_view:contentPadding="0dp">
        <fragment
            android:id="@+id/place_autocomplete_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Place"
            android:background="#fff"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            />
            </android.support.v7.widget.CardView>

Thanks in advance

Answer

user5248371 picture user5248371 · May 3, 2016

Use getActivity() like this way.

PlaceAutocompleteFragment autocompleteFragment1  = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);