繁体   English   中英

Android 23 API IllegalArgumentException错误

[英]Android 23 API IllegalArgumentException error

我收到此错误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

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) 

如果provider为null或在此设备上不存在,或者如果listener为null,则抛出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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM