【发布时间】:2017-02-11 22:47:28
【问题描述】:
ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATON 都已包含在清单中。活动中还包括:
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 或更高版本吗?你在哪里request permission at runtime?
标签: java android nullpointerexception android-location locationmanager