我正在嘗試爲我正在處理的應用程序獲取我的位置。但是,我收到一個錯誤,然後在嘗試選擇最佳提供程序時強制關閉。任何關於這個問題的幫助將不勝感激......我需要在onCreate中聲明一些東西以使其工作嗎?這裏是跟該錯誤的代碼片段:ANDROID:幫助LocationProvider
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運行。當我點擊打開獲取強制關閉位置的應用程序的地圖部分時。
我確實有這些權限確定...也粗略的位置。我只是沒有看到它出錯的地方... – 2011-04-16 23:42:19
這是模擬器還是真實設備?並且在仿真器/設備上啓用了提供程序嗎? – ddewaele 2011-04-16 23:46:57
呃。我遇到同樣的問題。我注意到谷歌的一件事情是,似乎有一個假設,如果有問題,他們的廢話將返回null,但我不確定這種行爲是否已經明確 – 2013-03-18 12:48:41