I have a string array of addresses in my res/strings. xml. I'm just trying to loop through them and add a marker to google maps but I'm getting a OutOfBoundsException on my array. Here's the error...
Process: com.gerhorgan.propertyprice, PID: 15147
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.gerhorgan.propertyprice.MapsActivity.onMapReady(MapsActivity.java:83)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:380)
at zu.a(:com.google.android.gms.DynamiteModulesB:82)
at maps.ad.t$5.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
here's my code..
package com.gerhorgan.propertyprice;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ZoomControls;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
ZoomControls zoom;
private String[] addressArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
zoom = (ZoomControls) findViewById(R.id.zcZoom);
zoom.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
});
zoom.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
addressArray= getResources().getStringArray(R.array.addresses);
Geocoder geocoder= new Geocoder(MapsActivity.this);
List<Address> addressList= null;
for(int i=0; i< addressArray.length; i++)
{
try {
addressList= geocoder.getFromLocationName(addressArray[i],1);
} catch (IOException e) {
e.printStackTrace();
}
Address address= addressList.get(0);
LatLng latlng= new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latlng).title(addressArray[i]));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
}
}
}