开发者

Android Crashing due to null Criteria

开发者 https://www.devze.com 2023-04-12 00:41 出处:网络
I have the following code that crashes due to the Criteria being null. For some reason the String best is null.Any ideas why?

I have the following code that crashes due to the Criteria being null.

For some reason the String best is null. Any ideas why?

private void startGPS() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String best = locationManager.getBestProvider(criteria, true);

    Log.v(DEBUG_TAG, "Best provid开发者_JAVA百科er: " + best);

    locationManager.requestLocationUpdates(best, 50, 0, this);

}


getBestProvider(criteria, true) will only return enabled providers. Since you request Criteria.ACCURACY_FINE you should get GPS, but if it is not enabled you'll get null.


From the documentation:

"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 [...]"

That means if the criteria is too strict then searches for the next best. So null will be returned if either no providers are enabled, or if you do not have the permission to access the provider.

Check your manifest. Also check the result of getProviders(); with false parameter, to see which providers may be available.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号