IllegalArgumentException: provider doesn't exi

2020-02-21 05:24发布

I'm using Google Maps API V1. I have this error :

     java.lang.IllegalArgumentException: provider doesn't exisit: null

This is my code :

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);

    if (provider != null)
    {

        startTime = System.currentTimeMillis(); 

        geoLocTimeOutTask = new GeoLocTimeOutTask();
        geoLocTimeOutTask.execute();

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
    else
    {
        geoLocCallbackObj.geoLocationCallback(tagCallback); 

    }

I understand the error, bu my question is, whay the device put me this error ? And how can I avoid this please ?

3条回答
来,给爷笑一个
2楼-- · 2020-02-21 06:03

the suggested was to fix this would be to use the new location API instead of the old one http://developer.android.com/google/play-services/location.html

but really all you have to do it do a check

if(locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER) && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

if the provider is not enabled then it wont pass and wont crash

查看更多
放荡不羁爱自由
3楼-- · 2020-02-21 06:11

When provider is not supported you get an error, the easiest way to get rid this problem is to use a Criteria.

locationManager.requestLocationUpdates(locationManager.(new Criteria(),true), 0, 0, locationListener);

In the code

criteria - the criteria that need to be matched

The second arg which is a boolean:

enabledOnly - if true hen only a provider that is currently enabled is returned

查看更多
爷、活的狠高调
4楼-- · 2020-02-21 06:21

You are requesting updates from the network provider when that provider does not exist on the device. You can replace these two lines:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

with

locationManager.requestLocationUpdates(provider, 0, 0, locationListener);

You've already gone to the effort of finding the best provider the device offers for your criteria so that is the one to use.

Alternatively, check the provider exists before registering for updates from it:

if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

You are also using zero for both time and minimum distance. This is perfect for testing on a virtual device but remember to change them when using a real device, otherwise battery drain will be high.

查看更多
登录 后发表回答