public void bcwens(View s)
{
String dz=Context.LOCATION_SERVICE;
LocationManager jt=(LocationManager) getSystemService(dz);
if (!jt .isProviderEnabled(android.location.LocationManager. GPS_PROVIDER));
String ddz=jt.GPS_PROVIDER;
Location zz=jt.getLastKnownLocation(ddz);
jt.requestLocationUpdates(ddz, 2000, 1, locationListener);
double z1=zz.getLatitude();//这两句不能运行直接崩了
double z2=zz.getLongitude();
String z3=String.valueOf(z1);
String z4=String.valueOf(z2);
jt.removeUpdates(locationListener);
MainActivity.writeNewsToSql(z3+z4);
}
private final LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location zz) {
getLocationInfo(zz);
}
@Override
public void onProviderDisabled(String provider) {
getLocationInfo(null);
}
@Override
public void onProviderEnabled(String provider) {
getLocationInfo(null);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
原因是location获取的值为空,导致的转换失败,至于为什么获取为空,还请大神来给我解答下
参考:http://www.2cto.com/kf/201209/157243.html
检查GPS是否正确获得了坐标,有时候GPS没有开启或者信号不好可能获取不到
你还没有定位成功,或者没有定位成功的缓存。这样Location读出来就是空的了。
先进行定位,然后这里做一个空指针判断保护。
public Location getLastKnownLocation (String provider)
Added in API level 1
Returns a Location indicating the data from the last known location fix obtained from the given provider.
This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
If the provider is currently disabled, null is returned.
所以首先检查一下,确定确实enable了GPS。 一般来说,即使当前GPS还没工作,也可以拿到历史的,除非你一直disable或者是刚factory reset了