Android Can';t捕获异常(GLS失败,状态为20)
我正试着定位一对又大又长的鞋子。当没有例外时,它工作得很好 日志:Android Can';t捕获异常(GLS失败,状态为20),android,exception-handling,gps,reverse-geocoding,Android,Exception Handling,Gps,Reverse Geocoding,我正试着定位一对又大又长的鞋子。当没有例外时,它工作得很好 日志: GLS Failed With Status 20 守则如下: //TAG GEOCODING METHOD public Address getAddressForLocation(Context context, Location location) throws IOException { Address a = null; try{ if (location == null
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;
}
日志的其余部分如下所示。这方面的任何帮助都会很好。我无法捕捉到这个异常,并且盯着这个代码看了这么久,仍然不知道我做错了什么
日志:
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
我认为问题在于您捕获了异常,但在
getAddressForLocation
catch (Exception e){
Log.w("EXCEPTON!", "Exception Caught in geocode");
a=null;
}
return a;
_addy = addy.getAddressLine(0);
因此,此时addy将为null:
addy = getAddressForLocation(this,arg0);
当你使用addy时,你会得到一个NullPointerException
catch (Exception e){
Log.w("EXCEPTON!", "Exception Caught in geocode");
a=null;
}
return a;
_addy = addy.getAddressLine(0);
在使用addy之前,您需要检查它是否为null,或者抛出您在
getAddressForLocation
中捕获的异常
,并在onLocationChange
中处理它,这正是原因所在。很明显,我就是看不见。谢谢你向我指出这一点。