如何从我在Android中的位置获取地址

时间:2016-08-14 16:19:25

标签: android location geocoding fatal-error

当我想从我的位置获取地址时,我遇到了问题。我不知道问题出在哪里,这是我的堆栈跟踪:

  

08-14 18:06:41.035 29629-29629 / com.cmn.cmnvtc E / AndroidRuntime:FATAL   例外:主要                                                                   处理:com.cmn.cmnvtc,PID:29629                                                                   java.lang.IndexOutOfBoundsException:索引0无效,大小为0                                                                       at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:260)                                                                       at java.util.ArrayList.get(ArrayList.java:313)                                                                       在com.cmn.cmnvtc.Trajetlibre $ 2.onClick(Trajetlibre.java:122)                                                                       在android.view.View.performClick(View.java:5265)                                                                       在android.view.View $ PerformClick.run(View.java:21534)                                                                       在android.os.Handler.handleCallback(Handler.java:815)                                                                       在android.os.Handler.dispatchMessage(Handler.java:104)                                                                       在android.os.Looper.loop(Looper.java:207)                                                                       在android.app.ActivityThread.main(ActivityThread.java:5728)                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

和:我的代码中存在问题的部分:

maPosition.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Geocoder gc = new Geocoder(getActivity());
                getLocation();
                List<Address> list = null;
                try {
                        list = gc.getFromLocation(lat, lng, 1);
                        if (list!= null) {
                                Address returnedAddress = list.get(0);
                                start_address.setText(returnedAddress.getAddressLine(0) + ", " + returnedAddress.getPostalCode() + " " + returnedAddress.getLocality());
                        } else {
                                start_address.setText("No Address returned!");
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                        start_address.setText("Cannot get address");
                }

        }
});

你能帮忙找到问题吗?

@Poger是这样的吗?

maPosition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       Geocoder gc = new Geocoder(getActivity());
                       List<Address> list = null;
                       try {
                           list = gc.getFromLocation(lat, lng, 1);
                           //  start_address.setText(Double.toString(lat));
                           if(list!= null && !list.isEmpty()) {
                               Address returnedAddress = list.get(0);
                               StringBuilder adressBuilder = new StringBuilder();
                               adressBuilder.append(returnedAddress.getAddressLine(0) + ", " + returnedAddress.getPostalCode() + " " + returnedAddress.getLocality());
                               getActivity().runOnUiThread(new Runnable() {
                                   @Override
                                   public void run() {
                                       start_address.setText("No Address returned!");
                                   }
                                   //job done on the UI thread
                               });
                           }
                           else{
                               getActivity().runOnUiThread(new Runnable() {
                                   @Override
                                   public void run() {
                                       start_address.setText("No Address returned!");
                                   }
                                   //job done on the UI thread
                               });
                           }
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   }
               }).start();

            }
        });

1 个答案:

答案 0 :(得分:0)

您的代码有2个问题。 让我们看一下documentation of getFromLocation method in Geocoder class。在返回部分中,它指出:

  

地址对象列表。如果未找到匹配项或没有可用的后端服务,则返回 null 空列表

您的代码仅处理空案例。它不检查返回的列表是否为空。稍后当你致电:list.get(0)时 - 你很容易碰到IndexOutOfBoundsException。

文档还说明:

  

从与主UI线程分开的线程调用此方法可能很有用。

看起来您正在从UI线程调用此方法。如你所见 - 你不应该。您应该在后台线程上调用getFromLocation,然后在UI线程上调用start_address.setText()

因此,首先您应该将代码更改为以下内容:

 list = gc.getFromLocation(lat, lng, 1);
 if (list != null && !list.isEmpty()) {
     Address returnedAddress = list.get(0);
     //...

其次,应在后台线程中调用getFromLocation(...)。您可以使用与此类似的代码来执行此操作:

new Thread(new Runnable() {
    public void run() {
         //job done on a background thread, e.g. getFromLocation() call
     }
}).start();

还有一件事 - 如果我正确地认为start_address是TextView的一个实例,那么应该在UI线程上调用start_address.setText("No Address returned!");。在后台线程上操作时,在Android中,实现它的方法之一是通过runOnUiThread() method of Activity class

getActivity().runOnUiThread(new Runnable() {
    //job done on the UI thread
});

您可以在this article中详细了解Android中的流程和主题。