小编Nee*_*lye的帖子

Google Places API AutocompleteSupportFragment空指针异常

我正在使用Google Places API来获取自动完成支持片段,以获取带有位置建议的搜索栏。我跟着这里给出的教程 商家信息自动填充

这是XML代码

<fragment
            android:id="@+id/autoCompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />
Run Code Online (Sandbox Code Playgroud)

这是Java代码

// Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.autoCompleteFragment);

    // Specify the types of place data to return.
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

        // Set up a PlaceSelectionListener to handle the response.
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        }
        @Override
        public void onError(Status status) {
            // TODO: Handle the error. …
Run Code Online (Sandbox Code Playgroud)

android google-places-api google-places-autocomplete

3
推荐指数
1
解决办法
1103
查看次数