Ad

Setting Place Fields Error While Using PlaceAutoCompleteFragment Using New Google Places SDK

Currently following Google's new guide on using Google Places using a Fragment however.

Despite setting

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

It brings up

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

Currently this is my code.

@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);
            }


        });

    }
Ad

Answer

Places SDK has been Depricated You need to migrate to new PLACES API. You can find solution by the below tguideline.

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

Ad
source: stackoverflow.com
Ad