Android IllegalArgumentException:提供者不存在:Maps V1 上的 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20660561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalArgumentException: provider doesn't exisit: null on Maps V1
提问by NigelK
I'm using Google Maps API V1. I have this error :
我正在使用 Google Maps API V1。我有这个错误:
java.lang.IllegalArgumentException: provider doesn't exisit: null
This is my code :
这是我的代码:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
if (provider != null)
{
startTime = System.currentTimeMillis();
geoLocTimeOutTask = new GeoLocTimeOutTask();
geoLocTimeOutTask.execute();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
else
{
geoLocCallbackObj.geoLocationCallback(tagCallback);
}
I understand the error, bu my question is, whay the device put me this error ? And how can I avoid this please ?
我理解错误,但我的问题是,为什么设备给我这个错误?我怎样才能避免这种情况?
回答by NigelK
You are requesting updates from the network provider when that provider does not exist on the device. You can replace these two lines:
当设备上不存在该提供商时,您正在请求该网络提供商的更新。您可以替换这两行:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
with
和
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
You've already gone to the effort of finding the best provider the device offers for your criteria so that is the one to use.
您已经努力寻找设备为您的标准提供的最佳供应商,以便使用该供应商。
Alternatively, check the provider exists before registering for updates from it:
或者,在注册更新之前检查提供者是否存在:
if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
You are also using zero for both time and minimum distance. This is perfect for testing on a virtual device but remember to change them when using a real device, otherwise battery drain will be high.
您还对时间和最小距离使用了零。这非常适合在虚拟设备上进行测试,但请记住在使用真实设备时更改它们,否则电池消耗会很高。
回答by tyczj
the suggested was to fix this would be to use the new location API instead of the old one http://developer.android.com/google/play-services/location.html
建议是解决这个问题是使用新的位置 API 而不是旧的http://developer.android.com/google/play-services/location.html
but really all you have to do it do a check
但实际上你要做的就是检查一下
if(locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER) && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
if the provider is not enabled then it wont pass and wont crash
如果未启用提供程序,则它不会通过并且不会崩溃
回答by Enzokie
When provider is not supported you get an error, the easiest way to get rid this problem is to use a Criteria.
当提供程序不受支持时,您会收到错误消息,解决此问题的最简单方法是使用 Criteria。
locationManager.requestLocationUpdates(locationManager.(new Criteria(),true), 0, 0, locationListener);
In the code
在代码中
criteria - the criteria that need to be matched
标准 - 需要匹配的标准
The second arg which is a boolean:
第二个 arg 是一个布尔值:
enabledOnly - if true hen only a provider that is currently enabled is returned
enabledOnly - 如果为 true,则仅返回当前启用的提供程序