Android 在安卓i';我在Geocoder中遇到异常
如果网络不可用或发生任何其他I/O问题,则会引发IOException。使用地理编码器需要互联网连接Android 在安卓i';我在Geocoder中遇到异常,android,Android,如果网络不可用或发生任何其他I/O问题,则会引发IOException。使用地理编码器需要互联网连接 public class MainActivity extends Activity { double LATITUDE = 37.42233; double LONGITUDE = -122.083; /** Called when the activity is first created. */ @Override public void onCr
public class MainActivity extends Activity {
double LATITUDE = 37.42233;
double LONGITUDE = -122.083;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
TextView myAddress = (TextView)findViewById(R.id.myaddress);
myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
e.printStackTrace();
myAddress.setText("Cannot get Address!");//and tell me what is
}
}
}
这似乎是许多人面临的问题。检查
如果使用Geocoder不起作用,即使有互联网连接,您也可以尝试使用。这似乎是许多人面临的一个问题。检查
如果使用Geocoder不起作用,即使存在internet连接,您也可以尝试使用。我重新启动Android设备,错误消失。我重新启动Android设备,错误消失。请为错误添加日志06-03 10:59:24.689:W/System.err(4129):java.io.IOException:Service not Available 06-03 10:59:24.689:W/System.err(4129):在android.location.Geocoder.getFromLocation(Geocoder.java:136)06-03 10:59:24.689:W/System.err(4129):在com.example.gprsonandoff.MainActivity.city(MainActivity.java:74)您尝试了不同的坐标吗?如果服务找不到坐标的任何地址,它将返回null。请为错误添加日志猫06-03 10:59:24.689:W/System.err(4129):java.io.IOException:服务不可用06-03 10:59:24.689:W/System.err(4129):在android.location.Geocoder.getFromLocation(Geocoder.java:136)06-03 10:59:24.689:W/System.err(4129):在com.example.gprsonandoff.MainActivity.city(MainActivity.java:74)上,您尝试使用不同的坐标?如果服务找不到坐标的任何地址,它将返回null。
06-03 10:59:24.689: W/System.err(4129):java.io.IOException: Service not Available
:at android.location.Geocoder.getFromLocation(Geocoder.java:136)
:at com.example.gprsonandoff.MainActivity.city(MainActivity.java:74)
:at com.example.gprsonandoff.MainActivity.onCreate(MainActivity.java:44)
:android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)