18

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

Amit Nair
  • 295
  • 2
  • 5
  • 21
  • 1
    Post your xml layout. – Rohit Arya May 03 '16 at 06:04
  • @Amit Nair see my answer below. –  May 03 '16 at 06:20
  • Can you post the full code here or git-hub and post a link. I'm working on the same [here](http://stackoverflow.com/questions/42549497/java-null-pointer-exception-on-google-places-fragment/42549726?noredirect=1#comment72235739_42549726) and running into numerous errors @Amit Nair – Nobody Mar 02 '17 at 08:52

6 Answers6

46

Use getActivity() like this way.

PlaceAutocompleteFragment autocompleteFragment1  = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);
  • 1
    @AmitNair glad to help you. –  May 03 '16 at 06:36
  • @AhmedWagdi use it inside fragment class. If you are not in Activity or fragment then pass the context to that class and use like this context.getFragmentManager() – Sutanu Rath Nov 18 '19 at 08:21
24
  1. Change SupportPlaceAutocompleteFragment instead of PlaceAutocompleteFragment in 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" 
    />
  1. Change getChildFragmentManager() instead of getFragmentManager() and use SupportPlaceAutocompleteFragment instead of PlaceAutocompleteFragment in your java file,.

     SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
    
Vicky
  • 5,098
  • 2
  • 33
  • 31
1

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"
/>
CacheMeOutside
  • 699
  • 7
  • 24
0

You have to use the following in your XML layout file:

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

Note the full classname and id, your id don't match. (place_autocomplete_fragment1)

sonique
  • 4,539
  • 2
  • 30
  • 39
0

The fragment id should be android:id="@+id/place_autocomplete_fragment1"

<fragment
android:id="@+id/place_autocomplete_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"/>
Naveen Shriyan
  • 1,266
  • 1
  • 9
  • 15
0

Another valid reason of this error is that you must be extending your Fragment class from android.support.v4.app.Fragment and casting it to PlaceAutoCompleteFragment which extends Fragment class from com.google.android.gms.location.PlaceAutocompleteFragment. To avoid this you should/can use SupportPlaceAutoCompleteFragment instead of you are using.

SupportPlaceAutocompleteFragment autocompleteFragment1 = (SupportPlaceAutocompleteFragment)
            getActivity.getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment1);

Using this your autocomplete widget will also work on the lower version devices.

Poras Bhardwaj
  • 1,073
  • 1
  • 15
  • 33