0

我试图让应用程序从纬度和经度转换为地址

问题是地理编码器类不起作用并引发异常

在下面我把 java 类和 xml 也和 log cat

    package com.example.androidfromlocation;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidFromLocation 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.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);

        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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            myAddress.setText("Canont get Address!");
        }

    }
}

这是xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AndroidFromLocation" >

 <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>
   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Location" android:background="#505050"/>
   <TextView android:id="@+id/mylatitude" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
   <TextView android:id="@+id/mylongitude" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Address" android:background="#505050"/>
   <TextView android:id="@+id/myaddress" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>

</RelativeLayout>

这也是原木猫

11-06 07:58:18.439: D/dalvikvm(16360): GC_CONCURRENT freed 86K, 2% free 6208K/6328K, paused 3ms+10ms, total 35ms
11-06 07:58:18.451: W/EGL_emulation(16360): eglSurfaceAttrib not implemented
11-06 08:08:23.003: E/Trace(17062): error opening trace file: No such file or directory (2)
11-06 08:08:23.031: D/AndroidRuntime(17062): Shutting down VM
11-06 08:08:23.035: W/dalvikvm(17062): threadid=1: thread exiting with uncaught exception (group=0xa6198908)
11-06 08:08:23.035: E/AndroidRuntime(17062): FATAL EXCEPTION: main
11-06 08:08:23.035: E/AndroidRuntime(17062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.azuraakbar.androidfromlocation/com.azuraakbar.androidfromlocation.AndroidFromLocation}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.os.Looper.loop(Looper.java:137)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at dalvik.system.NativeStart.main(Native Method)
11-06 08:08:23.035: E/AndroidRuntime(17062): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-06 08:08:23.035: E/AndroidRuntime(17062):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at java.util.ArrayList.get(ArrayList.java:304)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at com.azuraakbar.androidfromlocation.AndroidFromLocation.onCreate(AndroidFromLocation.java:36)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.Activity.performCreate(Activity.java:5104)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-06 08:08:23.035: E/AndroidRuntime(17062):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-06 08:08:23.035: E/AndroidRuntime(17062):    ... 11 more

提前致谢

4

1 回答 1

1

在访问 ArrayList 中的元素之前使用 ArrayList 大小检查,因为 如果没有找到匹配项或没有可用的后端服务,Geocoder.getFromLocation可能会返回 null 或空列表。

  if(addresses != null && addresses.size()>0) {
        Address returnedAddress = addresses.get(0);
        //your code here...
     }
于 2013-11-06T01:18:18.017 回答