31

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

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

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

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?

6 Answers 6

53

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

17
  • Yes, I know that works, but what I wanted to know is why I need to implement those methods, when the documentation says they have a default implementation, since the code compiles without them. And why the error behavior is different on different phones.
    – Bsa0
    Commented Nov 2, 2020 at 11:04
  • 2
    Not sure about it, buy I guess it is due to the changes in location listner class. Old android versions needed to define these functions in order to make them not abstract. Recently Android has made all three functions as non abstract and deprecated onStatusChaged(). See developer.android.com/sdk/api_diff/30/changes/…
    – mohit48
    Commented Nov 3, 2020 at 14:22
  • 2
    I hadn't implemented onProviderEnabled method and I got the same error (AbstractMethodError), I have implemented it and cleared the super method and now no more crashes
    – Ahmet B.
    Commented Nov 3, 2020 at 18:29
  • 1
    @ahmet locationListner class is an interface. So i dont think you should be using super in its functions.
    – mohit48
    Commented Nov 5, 2020 at 12:46
  • 2
    i have updated my thread to help others with a solution. stackoverflow.com/questions/65700139/… Thanks @mohit48
    – Panagiss
    Commented Jan 20, 2021 at 10:05
29

I was not completely satisfied with the answers above and decided to elaborate a little.

As @sunlover3 rightfully stated, the problematic methods' implementation was changed. Before API 30 they were abstract, which means that if the compileSdk would be anything but 30 the OP's code would give compile error (these methods must have been implemented). But in the API 30 they added empty default methods' implementation, which means that if the compileSdk is 30, the compiler checks these methods and gives no error, they are already implemented.

BUT, if one runs an app with compileSdk 30 on an older OS with the version below 30, these methods' implementation is still old, i.o. there is none, they are abstract! Hence, the error.

Honestly, that's an obvious SDK's devs error, such things must not happen. At worst there should've been a compile-time warning that one should still override these methods for the older versions compatibility. I hope someone less lazy than I am on this topic will report this to google tracker :)

7

Here's my situation. On Android 9 (API 28) and Android 10 (API 29), in the android.location.LocationManager class there is this method:

private void _handleMessage(Message msg) {
    switch (msg.what) {
        case TYPE_LOCATION_CHANGED:
            Location location = new Location((Location) msg.obj);
            mListener.onLocationChanged(location);
            break;
        case TYPE_STATUS_CHANGED:
            Bundle b = (Bundle) msg.obj;
            String provider = b.getString("provider");
            int status = b.getInt("status");
            Bundle extras = b.getBundle("extras");
            mListener.onStatusChanged(provider, status, extras);
            break;
        case TYPE_PROVIDER_ENABLED:
            mListener.onProviderEnabled((String) msg.obj);
            break;
        case TYPE_PROVIDER_DISABLED:
            mListener.onProviderDisabled((String) msg.obj);
            break;
    }
    locationCallbackFinished();
}

which seems to be called. Also, the difference of implementation for LocationListener between Android 29 and Android 30 is this:

Android 29:
public interface LocationListener {
    void onLocationChanged(Location location);
    
    @Deprecated
    void onStatusChanged(String provider, int status, Bundle extras);

    void onProviderEnabled(String provider);

    void onProviderDisabled(String provider);
}

Android 30:
public interface LocationListener {
    void onLocationChanged(@NonNull Location location);

    @Deprecated
    default void onStatusChanged(String provider, int status, Bundle extras) {}

    default void onProviderEnabled(@NonNull String provider) {}

    default void onProviderDisabled(@NonNull String provider) {}
}

It's obvious that the word "default" is the difference. Given that I have the same problem as you, I need to find a workaround now. Or use the response of mohit48. Good luck!

4

Override the methods in your code. ...

override fun onProviderDisabled(provider: String) {}
override fun onProviderEnabled(provider: String) {}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
...
3

I faced the same issue and I managed to solve it by overriding the onStatusChanged() as suggested above.

However, the Android team might be working on something and I found that there is this interface that solves the problem without overriding, as far as I have tested.

You can see here the summary for the androidx.core.location package.

In order to use these new classes in your project you should use the latest beta version of the androidx.core. Read here.

2

Just Add implement LocationListener

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

}

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

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    
}
2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 24, 2022 at 7:49
  • How does your answer differ from stackoverflow.com/a/64643361/4862968? Please do not post duplicates or add more details/explanations.
    – Antoine
    Commented Nov 26, 2022 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.