Android LocationListener,onStatusChanged和onProviderDisabled上的AbstractMethodError [英] Android LocationListener, AbstractMethodError on onStatusChanged and onProviderDisabled

查看:110
本文介绍了Android LocationListener,onStatusChanged和onProviderDisabled上的AbstractMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的简单活动,该按钮使用LocationManager尝试获取当前位置:

I have a simple activity with a button, that uses the LocationManager to try to get the current location:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonGps = findViewById(R.id.buttonGps);

    final Context activity = this;

    buttonGps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!checkForPhonePermission()) {
                return;
            }

            LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);

            locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
                @Override
                public void onLocationChanged(@NonNull Location location) {
                    Log.d(TAG, location.getLatitude() + "");
                }
            }, Looper.myLooper());
        }
    });
}

我在API级别为22的Android Studio中创建了一个模拟器,并在授予许可权并启用了模拟器gps的情况下,单击按钮时该应用崩溃,并显示此错误:

I created an emulator in Android Studio with the API level 22, and after giving the permission, and with the gps of emulator on, the app crashes with this error when clicking the button:

java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)"

如果我在gps关闭的情况下按下按钮,则会收到此错误消息:

If I press the button with the gps turned off, I get this error instead:

java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)"

如果我在小米Mi A1上尝试该应用程序,则仅在gps关闭并单击按钮时它会崩溃,而在gps开启并按下按钮时它不会崩溃.

If I try the app on my Xiaomi Mi A1, it only crashes when the gps is off and the button is clicked, it does not crash when the gps is on and the button is pressed.

从文档中,这些方法都标有默认值,所以我应该不需要实现它们.

From the documentation, those methods are marked with default, so I should not need to implement them.

这种行为有什么原因吗?

Is there any reason for this behavior?

推荐答案

只需在代码末尾添加以下三个函数:

just add these three function at the end of your code:

@Override
public void onProviderEnabled(@NonNull String provider) {

}

@Override
public void onProviderDisabled(@NonNull String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    
}

并编写您要在状态更改时执行的代码

and write your code that you would like to execute when status changes

这篇关于Android LocationListener,onStatusChanged和onProviderDisabled上的AbstractMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆