Home > Blockchain >  Class 'Anonymous class derived from PlaceSelectionListener' must either be declared abstra
Class 'Anonymous class derived from PlaceSelectionListener' must either be declared abstra

Time:10-05

i want to use Place Autocomplete So i read google documentation and i used 'Embed an AutocompleteSupportFragment' option but i typed exactly same, that wrote by google but there's error like this

  1. Class 'Anonymous class derived from PlaceSelectionListener' must either be declared abstract or implement abstract method 'onPlaceSelected(Place)' in 'PlaceSelectionListener'
  2. Method does not override method from its superclass

i did override method 'onPlaceSelected' but when i typed the code, the code become gray color and red line created at PlaceSelectionListener below..

i don't know why so plz help...

code here

public class FinalTest extends FragmentActivity {
    private static int AUTOCOMPLETE_REQUEST_CODE = 1;
    private static final String TAG = "FinalTest";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final_test);

        Places.initialize(getApplicationContext(), "my_api_key");
        PlacesClient placesClient = Places.createClient(this);

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

        // Specify the types of place data to return.
        autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);

        autocompleteFragment.setLocationBias(RectangularBounds.newInstance(
                new LatLng(-33.880490, 151.184363),
                new LatLng(-33.858754, 151.229596)));
        autocompleteFragment.setCountries("IN");
        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.
                Log.i(TAG, "An error occurred: "   status);
            }
        });
    }
}

CodePudding user response:

You didn't show your imports, but I guess you have this import:

import com.google.android.gms.location.places.ui.PlaceSelectionListener;

Change it to:

import com.google.android.libraries.places.widget.listener.PlaceSelectionListener;

  • Related