Skip to content Skip to sidebar Skip to footer

Android Java.lang.abstractmethoderror On Requestlocationupdates

i get this error java.lang.AbstractMethodError: abstract method 'void android.location.LocationListener.onProviderDisabled(java.lang.String) when i call locationManager.requestLoca

Solution 1:

override the methods in your code

...

overridefunonProviderDisabled(provider: String) {}
overridefunonProviderEnabled(provider: String) {}
overridefunonStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
...

Solution 2:

Found the solution. After android 10(Q) you won't need lot's of the the unimplemented methods of LocationListener like onProviderDisabled.

But most of our apps have high "margin" of backwards compatibility supporting much older Android versions so you will need to override these ones.

So in order to implement these we can do this by two approaches:

First solution is creating a new LocationListener as a variable and implement these methods straight away

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = newLocationListener() {
            @OverridepublicvoidonLocationChanged(@NonNull Location location) {
                System.out.println("DEBUG 1");
                Toast.makeText(MainActivity.this,location.getLatitude()+" "+location.getLongitude(),Toast.LENGTH_LONG).show();
            }

            @OverridepublicvoidonProviderEnabled(@NonNullString provider) {
                System.out.println("DEBUG 2");
                Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
            }

            @OverridepublicvoidonProviderDisabled(@NonNullString provider) {
                System.out.println("DEBUG 3");
                Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_LONG).show();
            }

            @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
                System.out.println("DEBUG 4");
                Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_LONG).show();

            }
        };
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0.5f, locationListener);

A Second approach is to implement LocationListener in your class and implement these methods

publicclassMainActivityextendsAppCompatActivityimplementsLocationListener

And now you must implement the methods

@Override
    public void onLocationChanged(@NonNull Location location) {

    }

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

    }

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

    }

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

    }

And your LocationListener is your own class

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0.5f, this);

Last thing to take in mind is that even tho public void onStatusChanged(String provider, int status, Bundle extras) {} is deprecated you SHOULD implement because as i said Android phones that use Android 9 or lower are going to trigger that method when taking location updates with locationManager.requestLocationUpdates

Post a Comment for "Android Java.lang.abstractmethoderror On Requestlocationupdates"