【发布时间】: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 && addressList.size() > 0) {那样检查条件。 -
.size() 方法无法在其中解析。
-
@NabiaSaroosh 改用
length。
标签: android xml google-maps android-layout arraylist