2016-06-18 6 views
0

Я получаю эту ошибку java.lang.IllegalArgumentException: invalid provider: null,Android ошибка IllegalArgumentException 23 API

В следующей строке - locationManager.requestLocationUpdates(bestProvider, 20000, 50, (LocationListener) this); я скомпилировать проект с 23 API. но когда я пытаюсь использовать API 21, проблем нет. может ли кто-нибудь помочь мне понять, почему это произошло, и как я могу это исправить.

Спасибо.

Edit:

// ///Criteria ////////// 

    Criteria criteria = new Criteria(); 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) { 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    }else{ 
     criteria.setAccuracy(Criteria.ACCURACY_MEDIUM); 
    } 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    String bestProvider = locationManager.getBestProvider(criteria, true); 


    locationManager.requestLocationUpdates(bestProvider, 20000, 50, (LocationListener) this); // (in miliseconds) // (in meters) update location 
+0

'bestProvider' является недействительным. Добавьте код, в который вы установите эту переменную. – tynn

+0

Хорошо, но я установил свой лучшийProvider следующим образом: String bestProvider = locationManager.getBestProvider (критерии, true); так почему bestProvider имеет значение null и как его исправить? – kbok

+0

Необходимо выполнить одно из разрешений ACCESS_COARSE_LOCATION или ACCESS_FINE_LOCATION во время выполнения, начиная с Android 6 (API 23). –

ответ

0

LocationManager апи 24 вызовов:

public void requestLocationUpdates(String provider, long minTime, 
            float minDistance, LocationListener listener) 
public void requestLocationUpdates(String provider, long minTime, 
            float minDistance, LocationListener listener, 
            Looper looper) 
public void requestLocationUpdates(long minTime, float minDistance, 
            Criteria criteria, LocationListener listener, 
            Looper looper) 
public void requestLocationUpdates(String provider, long minTime, 
            float minDistance, PendingIntent intent) 
public void requestLocationUpdates(long minTime, float minDistance, 
            Criteria criteria, PendingIntent intent) 

бросками IllegalArgumentException если поставщик является нулевым или не существует на этом устройстве ИЛИ если слушатель имеет значение

как ручка ??? example1:

try { 
     LocationManager.requestLocationUpdates(args...); 
    } catch (IllegalArgumentException iae) { 
      // handle exception here 
     } 
    } 

example2:

String provider = (LocationManager.getBestProvider(args...); 
    boolean allow = provider != null && listener != null 
    if(allow) LocationManager.requestLocationUpdates(args...); 
    else // scream yell log etc... 

Источники:

/** 
* Returns the name of the provider that best meets the given criteria. Only providers 
* that are permitted to be accessed by the calling activity will be 
* returned. If several providers meet the criteria, the one with the best 
* accuracy is returned. If no provider meets the criteria, 
* the criteria are loosened in the following sequence: 
* 
* <ul> 
* <li> power requirement 
* <li> accuracy 
* <li> bearing 
* <li> speed 
* <li> altitude 
* </ul> 
* 
* <p> Note that the requirement on monetary cost is not removed 
* in this process. 
* 
* @param criteria the criteria that need to be matched 
* @param enabledOnly if true then only a provider that is currently enabled is returned 
* @return name of the provider that best matches the requirements 
*/ 
public String getBestProvider(Criteria criteria, boolean enabledOnly) 

/** 
* Register for location updates using the named provider, and a 
* pending intent. 
* 
* <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} 
* for more detail on how to use this method. 
* 
* @param provider the name of the provider with which to register 
* @param minTime minimum time interval between location updates, in milliseconds 
* @param minDistance minimum distance between location updates, in meters 
* @param listener a {@link LocationListener} whose 
* {@link LocationListener#onLocationChanged} method will be called for 
* each location update 
* 
* @throws IllegalArgumentException if provider is null or doesn't exist 
* on this device 
* @throws IllegalArgumentException if listener is null 
* @throws RuntimeException if the calling thread has no Looper 
* @throws SecurityException if no suitable permission is present 
*/ 
@RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) 
public void requestLocationUpdates(String provider, long minTime, float minDistance, 
     LocationListener listener) 
Смежные вопросы