【问题标题】:onClick method is not workingonClick 方法不起作用
【发布时间】:2016-09-09 08:36:46
【问题描述】:

我想在 android 按钮中使用 onClick 方法,但它不能正常工作。另一方面,当我使用onClickListener 时,它会向我显示另一个错误,例如

java.lang.IndexOutOfBoundsException: 无效索引 0,大小为 0

我找不到它有什么问题。

Java 代码如下:

public void onSearch(View view) throws IOException{

        EditText sLocation = (EditText) view.findViewById(R.id.editText1);
         String location = sLocation.getText().toString();
         List<android.location.Address> addressList= null;

        if (location != null || !location.equals(""))
        {
            Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
            try {
                addressList = geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }


            android.location.Address address = addressList.get(0);
            LatLng latlng = new LatLng(address.getLatitude(),address.getLatitude());
            gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
            gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
        }

    }

add_masjid.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#000000">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentStart="true"
        android:text="@string/xyz"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        />

    <Button
        android:id="@+id/generalId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rect1"
        android:onClick="onSearch"
        android:text="@string/abc"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:clickable="true" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/textView1"
        android:layout_toStartOf="@+id/generalId"
        android:ems="10"
        android:inputType="text"
        android:background="#FFFFFF"
        android:labelFor="@+id/editText1"
        android:layout_above="@+id/map"
        android:layout_alignParentTop="true" />

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/generalId">

    </com.google.android.gms.maps.MapView>


</RelativeLayout>

【问题讨论】:

  • addressList 是一个空列表,你不能从中得到零索引,检查列表的大小,如果大于 0 则继续。
  • if (addressList != null &amp;&amp; addressList.size() &gt; 0) {那样检查条件。
  • .size() 方法无法在其中解析。
  • @NabiaSaroosh 改用length

标签: android xml google-maps android-layout arraylist


【解决方案1】:

检查您的addressList。它可能是空的。这就是你得到的原因

java.lang.IndexOutOfBoundsException: 无效索引 0,大小为 0

【讨论】:

  • 在我的情况下,如何在地址列表中传递值?
  • 你不应该在 addressList 中传递值,但是当它从设备中找到位置时它会被填充。所以你需要为此编写回调。即当位置可用时执行您的任务。
  • 这种情况下怎么写回调?
  • 参考this。您还会在 google 搜索中找到很多示例
【解决方案2】:

onClick 方法与问题无关。来自getFromLocationName 的文档:

[...]如果未找到匹配项或存在匹配项,则返回 null 或空列表 没有可用的后端服务。

因此,您也很有可能获得NPE。执行null 和空位检查以避免此类错误。

示例解决方案:

public void onSearch(View view) throws IOException {

    EditText sLocation = (EditText) view.findViewById(R.id.editText1);
    String location = sLocation.getText().toString();
    List<android.location.Address> addressList = null;

    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
        try {
            addressList = geocoder.getFromLocationName(location, 1);
            if (addressList != null && !addressList.isEmpty()) {
                android.location.Address address = addressList.get(0);
                LatLng latlng = new LatLng(address.getLatitude(), address.getLatitude());
                gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
            } else {
                Toast.makeText(getActivity(), "Failed to resolve address", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意:此代码将阻塞,直到 getFromLocationName 方法返回。在主线程上这样做是个坏主意。

【讨论】:

  • 它显示“无法解析地址”但我想解决它。我该怎么做?
  • 请建议我该怎么做?
  • @NabiaSaroosh 我刚刚通过将“位置”字符串设置为我在柏林的家庭地址(不提供国家/地区)来测试代码。它已正确地将国家解析为“德国”。我猜它根据您的输入找不到任何东西。使用不同的搜索查询多次尝试。
  • 那么,我该怎么办?
  • 好吧,我没有过多地使用Geocoder,这个答案旨在解决您的代码中的错误。尝试不同的搜索词并增加 maxResutls 参数,例如 geocoder.getFromLocationName(location, 5)
猜你喜欢
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 2011-11-17
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多