无法使用地理编码器,未处理的类型为IOException

joe*_*guy 0 android

这是代码

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
                Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                double longitude = location.getLongitude();
                double latitude = location.getLatitude();
                Geocoder gcd = new Geocoder(context, Locale.getDefault());
                List<Address> addresses = **gcd.getFromLocation(latitude, longitude, 1);**
Run Code Online (Sandbox Code Playgroud)

出现错误的部分用星号突出显示。

谢谢。

Ken*_*olf 5

getFromLocation()IOException如果网络不可用或发生任何其他I / O问题,则抛出。

http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation(double,double,int)

要解决此问题,请在其周围加上一个try/catch块:

try {
    List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
}
catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)