-1

我正在尝试使用地理编码器通过传递纬度和经度来获取位置。但我总是得到一个数组大小为零和错误indexoutofboundexception index 0 size 0

我尝试了不同的经度和纬度值。

    btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // create class object
                gps = new GPSTracker(MainActivity.this);

                // check if GPS enabled
                if (gps.canGetLocation()) {

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();


                    List<Address> address;

                    Geocoder gcd = new Geocoder(getBaseContext(), Locale
                            .getDefault());

                    try {
                        address = gcd.getFromLocation(latitude, longitude, 1);
                        if (address != null && address.size() >= 0) {
                            loc = address.get(0).getAdminArea();

                            Toast.makeText(
                                    getApplicationContext(),
                                    "Your Location is - \nLat: " + latitude
                                            + "\nLong: " + longitude + "\nLocation: "
                                            + loc, Toast.LENGTH_LONG).show();

                        } else
                            Toast.makeText(getApplicationContext(), "no data",
                                    Toast.LENGTH_LONG).show();

                        // System.out.println("size of ade " + address.size());
                        // System.out.println(address.get(0).getLocality());
                        // loc = address.get(0).getLocality();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // \n is for new line
//                  Toast.makeText(
//                          getApplicationContext(),
//                          "Your Location is - \nLat: " + latitude
//                                  + "\nLong: " + longitude + "\nLocation: "
//                                  + loc, Toast.LENGTH_LONG).show();
                } else {
                    // can't get location
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    gps.showSettingsAlert();
                }

            }
        });

这是堆栈跟踪

03-26 14:01:06.592: E/AndroidRuntime(11006): FATAL EXCEPTION: main
03-26 14:01:06.592: E/AndroidRuntime(11006): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-26 14:01:06.592: E/AndroidRuntime(11006):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at java.util.ArrayList.get(ArrayList.java:304)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at com.example.gpstracker.MainActivity$1.onClick(MainActivity.java:54)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at android.view.View.performClick(View.java:4204)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at android.view.View$PerformClick.run(View.java:17355)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at android.os.Handler.handleCallback(Handler.java:725)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at android.os.Looper.loop(Looper.java:137)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at java.lang.reflect.Method.invokeNative(Native Method)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at java.lang.reflect.Method.invoke(Method.java:511)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-26 14:01:06.592: E/AndroidRuntime(11006):    at dalvik.system.NativeStart.main(Native Method)

我的错误是什么?谢谢。

4

1 回答 1

1

改变

if (address != null && address.size() >= 0) {

 if (address != null && address.size() > 0) {
于 2013-03-26T14:50:46.993 回答