“从 PlaceSelectionListener 派生的匿名类”类必须声明为抽象或实现抽象方法

Posted

技术标签:

【中文标题】“从 PlaceSelectionListener 派生的匿名类”类必须声明为抽象或实现抽象方法【英文标题】:Class 'Anonymous class derived from PlaceSelectionListener' must either be declared abstract or implement abstract method 【发布时间】:2021-11-24 15:02:19 【问题描述】:

我想使用 Place Autocomplete 所以我阅读了谷歌文档 我使用了“嵌入 AutocompleteSupportFragment”选项 但我输入的完全一样,是谷歌写的,但是有这样的错误

    类“派生自 PlaceSelectionListener 的匿名类”必须声明为抽象或在“PlaceSelectionListener”中实现抽象方法“onPlaceSelected(Place)” 方法不会覆盖其超类中的方法

我确实覆盖了方法“onPlaceSelected”,但是当我输入代码时,代码变为灰色并在下面的 PlaceSelectionListener 处创建红线..

我不知道为什么,请帮忙...

代码在这里

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

【问题讨论】:

【参考方案1】:

你没有显示你的导入,但我猜你有这个导入:

导入 com.google.android.gms.location.places.ui.PlaceSelectionListener;

改成:

进口 com.google.android.libraries.places.widget.listener.PlaceSelectionListener;

【讨论】:

非常感谢。我已经考虑了几天,但我无法解决。谢谢。!!!

以上是关于“从 PlaceSelectionListener 派生的匿名类”类必须声明为抽象或实现抽象方法的主要内容,如果未能解决你的问题,请参考以下文章