Android位置管理器返回NULL
我有一个简单的位置管理器,它正常工作,但是当Android设备关闭后又重新打开时,即使我请求更新,Android位置管理器也会返回Null。我知道getLastKnownLocation可以返回null,但是我相信我正在代码中处理它。感谢所有建议 显然lon=location.getLongitude();它正在崩溃Android位置管理器返回NULL,android,nullpointerexception,latitude-longitude,locationmanager,Android,Nullpointerexception,Latitude Longitude,Locationmanager,我有一个简单的位置管理器,它正常工作,但是当Android设备关闭后又重新打开时,即使我请求更新,Android位置管理器也会返回Null。我知道getLastKnownLocation可以返回null,但是我相信我正在代码中处理它。感谢所有建议 显然lon=location.getLongitude();它正在崩溃 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
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位置管理器请求更新,它也会返回Null
对。请求更新请求Android开始尝试查找设备的位置。那需要一段时间。同时,getLastKnownLocation()
可以很好地返回null
getLastKnownLocation()
通常仅在您想知道位置时才有用,但如果该数据尚未准备好,您可以不使用它继续前进。如果需要该位置,并且getLastKnownLocation()
返回null
,则需要等待使用位置
,直到调用onLocationChanged()
。请注意,可能永远不会调用onLocationChanged()
,因为不要求用户启用任何位置提供程序,也不要求设备能够实际获得位置修复
即使我让android位置管理器请求更新,它也会返回Null
对。请求更新请求Android开始尝试查找设备的位置。那需要一段时间。同时,getLastKnownLocation()
可以很好地返回null
getLastKnownLocation()
通常仅在您想知道位置时才有用,但如果该数据尚未准备好,您可以不使用它继续前进。如果需要该位置,并且getLastKnownLocation()
返回null
,则需要等待使用位置
,直到调用onLocationChanged()
。请注意,可能永远不会调用onLocationChanged()
,因为不要求用户启用任何位置提供程序,也不要求设备能够实际获得位置修复。唯一的问题是我在Android手机上启用了飞行模式。我现在有点尴尬 唯一的问题是我在安卓手机上启用了飞行模式。我现在有点尴尬 我在飞行模式中看到了您的回复,但是您的代码中确实存在错误的做法
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();
}
您已经在这里看到了null中的位置,这意味着您无法访问诸如纬度和经度之类的内部属性。在任何情况下,位置请求都是异步的,启动它们需要很短的时间。 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();
}
}
即使在这里,在“else”clouse中,您也无法立即访问该位置。这意味着您无法立即获取位置(它将只保留最后一个值)。
您应该做的是实现onLocationChanges方法,并以异步方式在那里保存位置。像这样:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
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();
}
您已经在这里看到了null中的位置,这意味着您无法访问诸如纬度和经度之类的内部属性。在任何情况下,位置请求都是异步的,启动它们需要很短的时间。 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();
}
}
即使在这里,在“else”clouse中,您也无法立即访问该位置。这意味着您无法立即获取位置(它将只保留最后一个值)。
您应该做的是实现onLocationChanges方法,并以异步方式在那里保存位置。像这样:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
}
else
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lon = location.getLongitude();
}
这是一个愚蠢的问题,但是仍然有你添加的权限这是一个愚蠢的问题,但是仍然有你添加的权限非常感谢你的建议,我一定会适当地更改我的代码!非常感谢您的建议,我一定会适当地更改我的代码!