无法捕获异常(GLS 失败,状态 20)
我正在尝试地理定位一对经纬度。当没有例外时,它的效果很好。
Logcat:
GLS Failed With Status 20
代码如下:
//TAG GEOCODING METHOD
public Address getAddressForLocation(Context context, Location location) throws IOException {
Address a = null;
try{
if (location == null) {
a=null;
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int maxResults = 1;
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
if (addresses.size() == 1) {
a=addresses.get(0);
} else {
a=null;
}
}catch (Exception e){
Log.w("EXCEPTON!", "Exception Caught in geocode");
a=null;
}
return a;
}
该调用也包含在 try/catch 块中。
@Override//TAG On Location Changed
public void onLocationChanged(Location arg0) {
//SET ALL VALUES
dlat = arg0.getLatitude();
dlon = arg0.getLongitude();
delev = arg0.getAltitude() * 3.2808399;
delev = round(delev, 2);
//GET THE ADDRESS
try {
addy = getAddressForLocation(this,arg0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_lastaddy = _addy;
//DISPLAY ALL INFORMATION
_addy = addy.getAddressLine(0);
LONGBOX.setText(String.valueOf(dlon));
LATBOX.setText(String.valueOf(dlat));
if(_addy!=_lastaddy){
ADDRESS.setText(_addy);
}
}
logcat 的其余部分如下。对此的任何帮助都会很棒。我无法捕获这个异常,并且已经盯着这段代码这么久了,仍然不知道我做错了什么。
日志:
LocationMasfClient reverseGeocode(): GLS failed with status 20
AndroidRuntime Shutting down VM
dalvikvm threadid=1: thread exiting with uncaught exception (group=0x400208b0)
AndroidRunTime FATAL EXCEPTION: main
AndroidRunTime java.lang.NullPointerException
I am trying to geolocate a lat,long pair. When there is no exception it works great.
Logcat:
GLS Failed With Status 20
The code follows:
//TAG GEOCODING METHOD
public Address getAddressForLocation(Context context, Location location) throws IOException {
Address a = null;
try{
if (location == null) {
a=null;
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int maxResults = 1;
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
if (addresses.size() == 1) {
a=addresses.get(0);
} else {
a=null;
}
}catch (Exception e){
Log.w("EXCEPTON!", "Exception Caught in geocode");
a=null;
}
return a;
}
The call is also wrapped in a try/catch block.
@Override//TAG On Location Changed
public void onLocationChanged(Location arg0) {
//SET ALL VALUES
dlat = arg0.getLatitude();
dlon = arg0.getLongitude();
delev = arg0.getAltitude() * 3.2808399;
delev = round(delev, 2);
//GET THE ADDRESS
try {
addy = getAddressForLocation(this,arg0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_lastaddy = _addy;
//DISPLAY ALL INFORMATION
_addy = addy.getAddressLine(0);
LONGBOX.setText(String.valueOf(dlon));
LATBOX.setText(String.valueOf(dlat));
if(_addy!=_lastaddy){
ADDRESS.setText(_addy);
}
}
The rest of the logcat is below. Any help with this would be GREAT. I can not catch this exception and have stared at this code so long and still dont know what I have done wrong.
LOGCAT:
LocationMasfClient reverseGeocode(): GLS failed with status 20
AndroidRuntime Shutting down VM
dalvikvm threadid=1: thread exiting with uncaught exception (group=0x400208b0)
AndroidRunTime FATAL EXCEPTION: main
AndroidRunTime java.lang.NullPointerException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题是你捕获了异常,但随后在 getAddressForLocation 中返回(并最终尝试使用)一个 null Address 对象
因此,此时 addy 将为 null:
当你去使用 addy 时,你会得到一个
NullPointerException
你要么需要在使用之前检查 addy 是否为 null,要么抛出你在
getAddressForLocation
中捕获的Exception
并在onLocationChange
中处理它I think the problem is that you catch the exception, but then return (and eventually try to use) a null Address object in
getAddressForLocation
So, at this point addy will be null:
When you go to use addy, you will get a
NullPointerException
You either need to check if addy is null before you use it, or throw the
Exception
you catch ingetAddressForLocation
and handle it inonLocationChange