ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATON都包含在清單中。還包括在活動是:導致空指針的GPS精度不足
import android.location.Location;
import android.location.LocationManager;
我對導航抽屜監聽器與下面的類
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
}
selectItem(position);
}
}
由此看來,location = null
,因此將無法在location.getLongitude();
。我當然可以避免這個與if (location != null)
,但我想找到lat
和long
。
爲什麼位置始終爲空?
您的目標是API 23還是更高?你在哪裏[在運行時請求許可](https://developer.android.com/training/permissions/requesting.html)? – ianhanniballake