PlaceAutocompleteFragment在点击时消失

时间:2017-04-13 12:53:01

标签: android google-maps google-maps-android-api-2 autocompletetextview

我想实现AutocompleteTextView(google places),但是当我点击片段中的searchView时,片段就会消失(摔倒)。

我的代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autocomplete);

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
                .build();

        autocompleteFragment.setFilter(typeFilter);

        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

            @Override
            public void onPlaceSelected(com.google.android.gms.location.places.Place place) {
                // TODO: handle click
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
            }
        });

    }

的xml:

<RelativeLayout
    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"
    android:orientation="vertical"
    tools:context="ui.screen.activity.AutocompleteActivity">

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

</RelativeLayout>

感谢您的任何建议

1 个答案:

答案 0 :(得分:6)

我遇到了同样的问题。我在API控制台上搞砸了一下  https://console.developers.google.com/启用Maps API AND 适用于Android的Places API,现在它正常运行。