简体   繁体   English

为什么此已处理的异常导致强制关闭? [GeoCoder]

[英]Why is this handled exception causing a Force Close? [GeoCoder]

I have the following in my AsyncTask: 我的AsyncTask中包含以下内容:

@Override
protected Void doInBackground(Context... c) {

    try {
        context = c[0];

        myLocation = new Geocoder((Activity)context, Locale.getDefault()); 

        if(myLocation.isPresent())
            myList = myLocation.getFromLocation(LocationHandler.getLat(), LocationHandler.getLon(), 1);
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }

    return null;
}

If I run this AsyncTask with no network connection, my application will Force Close with the following stack output: 如果我在没有网络连接的情况下运行此AsyncTask,则我的应用程序将使用以下堆栈输出强制关闭:

 java.io.IOException: Unable to parse response from server
 at android.location.Geocoder.getFromLocation(Geocoder.java:136)
 at com.soyo.winnipegtransit.location.UpdateGeoCoderName.doInBackground(UpdateGeoCoderName.java:44)
 at com.soyo.winnipegtransit.location.UpdateGeoCoderName.doInBackground(UpdateGeoCoderName.java:1)
 at android.os.AsyncTask$2.call(AsyncTask.java:185)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 at java.lang.Thread.run(Thread.java:1019)

I'm not sure why this exception is going unhandled? 我不确定为什么无法处理此异常?

I've had similar problems with exceptions not being handled. 我也遇到过类似的问题,但未处理异常。 It usually got solved by just handling the exception with one catch statement, don't ask me how but I'm sure there's a good reason behind this. 通常只用一个catch语句处理异常就可以解决,不要问我怎么做,但是我敢肯定这背后有充分的理由。 Have you tried removing the catch(IOException e) and just keeping catch(Exception e) ? 您是否尝试过删除catch(IOException e)并仅保留catch(Exception e)

Ryan 瑞安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM