1

ネットワークが使用できない場合、またはその他の I/O 問題が発生した場合は、IOException がスローされます。Geocoder を使用するには、インターネット接続が必要です。

   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 
}

 }
}

誰でも何か考えがありますか?

私は Emulator 2.1 api 8 でそれを行うことができましたが、リバース ジオコーディングでは常に空の結果が返されます。誰かが私のコードを確認できますか?

丸太の猫

      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)
4

2 に答える 2

8

多くの人が直面する課題のようです。これを確認してください。

インターネットに接続されていても Geocoder を使用できない場合は、GeoCoding APIを使用してみてください。

于 2013-06-03T06:36:41.327 に答える
0

Android デバイスを再起動すると、エラーがなくなりました。

于 2016-09-25T14:34:05.027 に答える