无法使用android中的地理编码器获取地址

时间:2014-04-17 07:26:33

标签: android google-geocoder

  • 我正在尝试使用纬度和经度获取位置详细信息
  • 我使用模拟器运行此代码,因为我没有设备, 当我运行代码时,我得到错误,如日志
  • 中所示

MainActivity.java

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

        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");
                }

                Log.d("RESULT",strReturnedAddress.toString());
            }
            else{
                Log.d("NO-RESULT","NO-RESULT");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("NO-RESULT","NO-RESULT");
        }

    }

}

日志::

04-17 12:50:22.489: E/AndroidRuntime(609): FATAL EXCEPTION: main
04-17 12:50:22.489: E/AndroidRuntime(609): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.findingpresentlocation/com.example.findingpresentlocation.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.os.Looper.loop(Looper.java:137)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-17 12:50:22.489: E/AndroidRuntime(609):  at java.lang.reflect.Method.invokeNative(Native Method)
04-17 12:50:22.489: E/AndroidRuntime(609):  at java.lang.reflect.Method.invoke(Method.java:511)
04-17 12:50:22.489: E/AndroidRuntime(609):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-17 12:50:22.489: E/AndroidRuntime(609):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-17 12:50:22.489: E/AndroidRuntime(609):  at dalvik.system.NativeStart.main(Native Method)
04-17 12:50:22.489: E/AndroidRuntime(609): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-17 12:50:22.489: E/AndroidRuntime(609):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-17 12:50:22.489: E/AndroidRuntime(609):  at java.util.ArrayList.get(ArrayList.java:304)
04-17 12:50:22.489: E/AndroidRuntime(609):  at com.example.findingpresentlocation.MainActivity.onCreate(MainActivity.java:30)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.Activity.performCreate(Activity.java:4465)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-17 12:50:22.489: E/AndroidRuntime(609):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-17 12:50:22.489: E/AndroidRuntime(609):  ... 11 more

2 个答案:

答案 0 :(得分:1)

您只检查返回的列表是否为空,但它可能是空的。您可以在documentation中看到返回的列表可以为null或为空,因此也要进行大小检查:

if (addresses != null && !addresses.isEmpty())

此外,您不应该在主线程中执行此操作,例如使用一个AsyncTask。为什么解释here

  

该方法是同步的,可能需要很长时间才能完成其工作,所以   你应该从doInBackground()方法调用该方法   的AsyncTask。

答案 1 :(得分:0)

您的List<Address> addresses不为空,但列表中没有元素,因此大小为0.您可以像这样更改代码:

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

        Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

        try {
            List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

            if(addresses != null && addresses.size() > 0) {
                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");
                }

                Log.d("RESULT",strReturnedAddress.toString());
            }
            else{
                Log.d("NO-RESULT","NO-RESULT");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("NO-RESULT","NO-RESULT");
        }

    }

尝试在代码段中添加&& addresses.size() > 0