我有一個簡單的位置管理器,通常工作,但是當Android設備已被關閉,然後重新打開,android位置管理器返回空,即使我有它請求更新。我知道getLastKnownLocation可以返回null,但我相信我正在處理我的代碼。所有建議讚賞。Android位置管理返回NULL
顯然lon = location.getLongitude();正在崩潰。
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
// request location update!!
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lon = location.getLongitude();
lat = location.getLatitude();
}
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Get last known location
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Update if not null
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
//Request update as location manager can return null otherwise
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lat = location.getLatitude();
lon = location.getLongitude();
}
}
這是一個愚蠢的問題,但仍然有添加的權限<使用許可權的android:NAME = 「android.permission.ACCESS_FINE_LOCATION」/> <使用許可權的android:NAME =「機器人。 permission.INTERNET「/> – Soham
我有,是的! :D – ThemuRR