Adding PlaceAutocompleteFragment to fragment throws error

后端 未结 6 1284
死守一世寂寞
死守一世寂寞 2021-01-01 16:36

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

相关标签:
6条回答
  • 2021-01-01 17:03

    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"
    />
    
    0 讨论(0)
  • 2021-01-01 17:07
    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);
      
    0 讨论(0)
  • 2021-01-01 17:12

    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)

    0 讨论(0)
  • 2021-01-01 17:17

    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.

    0 讨论(0)
  • 2021-01-01 17:23

    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"/>
    
    0 讨论(0)
  • 2021-01-01 17:24

    Use getActivity() like this way.

    PlaceAutocompleteFragment autocompleteFragment1  = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment1);
    
    0 讨论(0)
提交回复
热议问题