使用新的Google Places SDK使用PlaceAutoCompleteFragment时设置“位置字段”错误

时间:2019-03-15 13:02:38

标签: java android google-maps google-places-api googleplacesautocomplete

但是,目前正在遵循Google的新guide使用片段使用Google地方信息。

尽管设置

autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

它弹出

 java.lang.NullPointerException: Place Fields must be set.

当前这是我的代码。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        autocompleteFragment.onActivityResult(requestCode, resultCode, data);


        autocompleteFragment = (AutocompleteSupportFragment)
                getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);

        if (!Places.isInitialized()) {
            Log.d(TAG, "onActivityResult: Start");
            Places.initialize(getActivity(), String.valueOf(R.string.google_api_key));

        }


        if (autocompleteFragment != null) {
            autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
        }

        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(@NonNull Place place) {
                Log.d(TAG, "Place: " + place.getName() + ", " + place.getId());

            }

            @Override
            public void onError(@NonNull Status status) {

                Log.d(TAG, "onError: Error:" + status);
            }


        });

    }

1 个答案:

答案 0 :(得分:1)

  

Places SDK已被废弃。您需要迁移到新的 PLACES API 。   您可以通过以下tguideline找到解决方案。

https://stackoverflow.com/a/55045772/10579969