我收到此错误java.lang.IllegalArgumentException: invalid provider: null
,
从下一行 - locationManager.requestLocationUpdates(bestProvider, 20000, 50, (LocationListener) this);
我使用23 API编译项目。但是当我尝试21 API时没有问题。任何人都可以帮助我理解为什么会这样,我该如何解决这个问题。
感谢。
修改
// ///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 :(得分:0)
LocationManager api 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
如何处理??? 例1:
try {
LocationManager.requestLocationUpdates(args...);
} catch (IllegalArgumentException iae) {
// handle exception here
}
}
例2:
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)