我試圖簡單地獲取用戶的GPS位置,但嘗試獲取設備的經度和緯度時,我的應用程序崩潰。對於發生錯誤見代碼..如何在Android(2.2)設備上獲取用戶的GPS位置
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
final double longitude = location.getLongitude(); //error occurs here
final double latitude = location.getLatitude(); //and here
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//longitude = location.getLongitude();
//latitude = location.getLatitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//update location via GPS PROVIDER
//can also use LocationManager.NETWORK_PROVIDER
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
我還包括在清單文件中的ACCESS_FINE_LOCATION許可AM:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
所以我不明白爲什麼這是行不通的。任何幫助將不勝感激!
如果您遇到崩潰,這意味着您會收到一個調用堆棧。你可能想要發佈它。 – EboMike 2011-02-11 02:41:51