我正在尝试获取我正在处理的应用程序的位置。但是,当它尝试选择最佳提供商时,我收到一个错误然后强制关闭。对此问题的任何帮助将不胜感激...我是否需要在 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 上运行的。当我单击打开应用程序的地图部分时,它会强制关闭它的位置。