Adding PlaceAutocompleteFragment to fragment throws error
Use getActivity()
like this way.
PlaceAutocompleteFragment autocompleteFragment1 = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);
- Change SupportPlaceAutocompleteFragment instead of
PlaceAutocompleteFragmentin your layout xml
fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Change getChildFragmentManager() instead of
getFragmentManager()and use SupportPlaceAutocompleteFragment instead ofPlaceAutocompleteFragmentin your java file,.SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
Here's the solution in Kotlin. This is specifically for if you want to launch the autocomplete fragment while being in a fragment.
val autocompleteFragment = childFragmentManager.findFragmentById(R.id.autocomplete_support_fragment) as AutocompleteSupportFragment?
<fragment
android:id="@+id/autocomplete_support_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>