开发者

Can't catch Exception (GLS FAILED WITH STATUS 20)

开发者 https://www.devze.com 2023-02-06 20:55 出处:网络
I am trying to geolocate a lat,long pair.When there is no exception it works great. Logcat: GLS Failed With Status 20

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.getLongitud开发者_Python百科e();

            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


I think the problem is that you catch the exception, but then return (and eventually try to use) a null Address object in getAddressForLocation

catch (Exception e){
      Log.w("EXCEPTON!", "Exception Caught in geocode");
      a=null;
     }
     return a;

So, at this point addy will be null:

addy = getAddressForLocation(this,arg0);

When you go to use addy, you will get a NullPointerException

_addy = addy.getAddressLine(0);  

You either need to check if addy is null before you use it, or throw the Exception you catch in getAddressForLocation and handle it in onLocationChange

0

精彩评论

暂无评论...
验证码 换一张
取 消