ANDROID:有关 LocationProvider 的帮助
我正在尝试获取我正在开发的应用程序的位置。但是,当它尝试选择最佳提供商时,我收到错误,然后强制关闭。对于这个问题的任何帮助将不胜感激...我是否需要在 onCreate 中声明一些内容才能使其工作?这是一段代码,后跟错误:
public void onStart(){
super.onStart();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = locationManager.getBestProvider(criteria, true);//Selects best location provider given options between GPS and poor man's
locationProvider = locationManager.getProvider(best);
if (locationProvider != null) {
locationManager.requestLocationUpdates(locationProvider.getName(), 60000, 1,
this.locationListenerRecenterMap);
} else {
Log.e(TAG, "NO LOCATION PROVIDER AVAILABLE");
Toast.makeText(this, "The GPS location provider is not available at this time.", Toast.LENGTH_SHORT).show();
finish();
}
GeoPoint location = this.getLastKnownPoint();
this.mapController.animateTo(location);
}
public void onResume(){
super.onResume();
locationManager.requestLocationUpdates(best, 15000, 1, (LocationListener) this);
}
public void onPause(){
locationManager.removeUpdates((LocationListener) this);
}
private GeoPoint getLastKnownPoint(){
GeoPoint lastKnownPoint = GeoUpdateHelper.SCRANTON;
Location lastKnownLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnownLocation != null){
lastKnownPoint = GeoUpdateHelper.getGeoPoint(lastKnownLocation);
}else{
lastKnownPoint = GeoUpdateHelper.SCRANTON;
}
return lastKnownPoint;
}
这是错误:
04-16 19:07:25.077: ERROR/AndroidRuntime(4998): Caused by: java.lang.IllegalArgumentException: name==null
04-16 19:07:25.077: ERROR/AndroidRuntime(4998): at android.location.LocationManager.getProvider(LocationManager.java:324)
04-16 19:07:25.077: ERROR/AndroidRuntime(4998): at com.example.mapMain.onStart(mapMain.java:76)
编辑:这在我的 OG Droid 上运行。当我单击打开应用程序的地图部分时,它会强制关闭获取位置的位置。
I am trying to acquire my location for an application I am working on. However I receive an error then force close when it tries to select the best provider. Any help on the issue would be much appreciated...do I need to declare something in onCreate in order for it to work? Here is a snippet of code followed by the error:
public void onStart(){
super.onStart();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = locationManager.getBestProvider(criteria, true);//Selects best location provider given options between GPS and poor man's
locationProvider = locationManager.getProvider(best);
if (locationProvider != null) {
locationManager.requestLocationUpdates(locationProvider.getName(), 60000, 1,
this.locationListenerRecenterMap);
} else {
Log.e(TAG, "NO LOCATION PROVIDER AVAILABLE");
Toast.makeText(this, "The GPS location provider is not available at this time.", Toast.LENGTH_SHORT).show();
finish();
}
GeoPoint location = this.getLastKnownPoint();
this.mapController.animateTo(location);
}
public void onResume(){
super.onResume();
locationManager.requestLocationUpdates(best, 15000, 1, (LocationListener) this);
}
public void onPause(){
locationManager.removeUpdates((LocationListener) this);
}
private GeoPoint getLastKnownPoint(){
GeoPoint lastKnownPoint = GeoUpdateHelper.SCRANTON;
Location lastKnownLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnownLocation != null){
lastKnownPoint = GeoUpdateHelper.getGeoPoint(lastKnownLocation);
}else{
lastKnownPoint = GeoUpdateHelper.SCRANTON;
}
return lastKnownPoint;
}
And here is the error:
04-16 19:07:25.077: ERROR/AndroidRuntime(4998): Caused by: java.lang.IllegalArgumentException: name==null
04-16 19:07:25.077: ERROR/AndroidRuntime(4998): at android.location.LocationManager.getProvider(LocationManager.java:324)
04-16 19:07:25.077: ERROR/AndroidRuntime(4998): at com.example.mapMain.onStart(mapMain.java:76)
EDIT: This is running on my OG Droid. When I click to open the map part of the application which gets the location it force closes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误表明未找到最佳提供程序,并且对 getBestProvider() 的调用返回 null。
尽管您没有真正指定任何标准,但人们会期望此调用至少返回“某些内容”。 javadoc 有点含糊,没有提到这个方法可以返回 null。 (查看 源代码,它实际上可以返回 null)
但是,您的应用/设备可能没有任何可用的提供程序。
您可以执行以下调用来检查它是否返回任何提供程序(它将返回所有启用的提供程序,不考虑任何条件)?
假设这是应用程序的限制,请仔细检查您是否定义了以下权限,因为它们将需要返回某种提供程序。
The error indicates that no best provider was found, and the call to getBestProvider() returned null.
Although you didn't really specify any criteria, one would expect this call to at least return "something". The javadocs are a bit vague, and don't mention that this method can return null. (looking at the source code, it actually can return null)
However, it is possible that your app / device doesn't have any providers available.
Can you execute the following call to check if it returns any providers (it will return all enabled providers, not taking into account any criteria) ?
Assuming this is a limitation of the application, double check if you have the following permissions defined, as they will be required to return some kind of provider.
就我而言,我在系统设置中未启用任何位置服务(Google 位置服务、独立 GPS 服务、VZW 位置服务)的设备上进行测试。因此
requestLocationUpdates
导致应用崩溃。我将该代码放在 try/catch 块中,应用程序不再崩溃。In my case I was testing on a device that didn't have any location services enabled in system settings (Google location services, Standalone GPS services, VZW location services). Therefore
requestLocationUpdates
was causing the app to crash. I surrounded that code in a try/catch block, and the app no longer crashed.