我试图简单地获取用户的 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 权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
所以我不太确定为什么这不起作用。任何帮助将不胜感激!