无法捕获异常(GLS未通过状态20)

时间:2011-01-24 00:19:35

标签: android exception-handling gps reverse-geocoding

我正在尝试对一对拉长对地理定位。如果没有例外,那就很有效。

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的其余部分如下。任何帮助都会很棒。我无法捕捉到这个异常并且长时间盯着这段代码,但仍然不知道我做错了什么。

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

1 个答案:

答案 0 :(得分:1)

我认为问题在于您捕获异常,但随后返回(并最终尝试使用)getAddressForLocation

中的null地址对象
catch (Exception e){
      Log.w("EXCEPTON!", "Exception Caught in geocode");
      a=null;
     }
     return a;

所以,此时addy将为null:

addy = getAddressForLocation(this,arg0);

当你去使用addy时,你会得到一个NullPointerException

_addy = addy.getAddressLine(0);  

您需要在使用之前检查addy是否为null,或者在Exception中抛出getAddressForLocation并在onLocationChange中处理它