java.lang.NoSuchMethodError: No virtual method getCurrentLocation

Viewed 509

When trying to fetch the current location which was introduced in api 30 we are getting following error any suggestion on this. Below is the code which we have tried.

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

 String provider = locationManager.getBestProvider(criteria, false);

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == 
   PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.getCurrentLocation(provider, null,new DirectExecutor(), new Consumer<Location>() {
                        @Override
                        public void accept(Location location) {

                            Log.i("test", "location -->"+location.getLatitude());

                        }
                    });
                }


    class DirectExecutor implements Executor {
            public void execute(Runnable r) {
                r.run();
            }
        }
1 Answers

Try invalidating Cache and restarting android studio, usually this resovles such issues.

Related