-4

I want to use onClick method in android button but it is not working correct. On the other hand when I am using onClickListener then it is showing me another error like

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

I couldn't find what is the problem with it.

Java code is given below :

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>
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
nabia saroosh
  • 399
  • 1
  • 14
  • 36

2 Answers2

0

check your addressList. It might be empty. That's why you are getting

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

Nikhil
  • 3,711
  • 8
  • 32
  • 43
0

the onClick method is unrelated to the issue. From the docs for getFromLocationName:

[...]Returns null or empty list if no matches were found or there is no backend service available.

So chances are good that you may also get an NPE. Do a null and an emptiness check to avoid such errors.

Sample solution:

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();
        }
    }
}

NOTE: this code would block until the getFromLocationName method returns. Doing so on the main Thread is a bad idea.

Community
  • 1
  • 1
Droidman
  • 11,485
  • 17
  • 93
  • 141