无法捕获异常(GLS 失败,状态 20)

发布于 2024-10-14 11:57:10 字数 2200 浏览 3 评论 0原文

我正在尝试地理定位一对经纬度。当没有例外时,它的效果很好。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿嘿嘿 2024-10-21 11:57:10

我认为问题是你捕获了异常,但随后在 getAddressForLocation 中返回(并最终尝试使用)一个 null Address 对象

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,要么抛出你在 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文