簡體   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