Nabia Saroosh Nabia Saroosh - 2 months ago 61
Android Question

Error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 in fragment by viewPager

I am using map in tab(viewPager) and I want to search location in map for this purpose I am using EditText field and array list but I am still getting this error. I couldn't find what's the problem with it. Please anybody suggest me, what should I do?

FATAL EXCEPTION: main Process: com.example.saroosh.masjidnow, PID: 10465
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.example.saroosh.masjidnow.Tabs.AddMasjid$1.onClick(AddMasjid.java:61) at
android.view.View.performClick(View.java:5184) at
android.view.View$PerformClick.run(View.java:20910) at
android.os.Handler.handleCallback(Handler.java:739) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:145) at
android.app.ActivityThread.main(ActivityThread.java:5942) 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:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)


My java class is given below.

AddMasjid.java

package com.example.saroosh.masjidnow.Tabs;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.Button;
import android.widget.EditText;

import com.example.saroosh.masjidnow.R;
import com.google.android.gms.identity.intents.Address;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
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 AddMasjid extends Fragment implements OnMapReadyCallback {

MapView gMapView;
GoogleMap gMap = null;
Button sButton;
String location;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_masjid, container, false);
EditText sLocation = (EditText) v.findViewById(R.id.editText1);
location = sLocation.getText().toString();
sButton = (Button) v.findViewById(R.id.generalId);
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

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

}
});

gMapView = (MapView) v.findViewById(R.id.map);
gMapView.getMapAsync(this);
gMapView.onCreate(savedInstanceState);
gMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}

return v;
}


@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(0, 0), 0));


if (gMap != null) {
gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext()
, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext()
, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
gMap.setMyLocationEnabled(true);
gMap.getUiSettings().setMyLocationButtonEnabled(true);


}
}
@Override
public void onResume() {
super.onResume();
gMapView.onResume();

}

public void onSearch(View view) throws IOException{

}

@Override
public void onPause() {
super.onPause();
gMapView.onPause();

}

@Override
public void onDestroy() {
super.onDestroy();
gMapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
gMapView.onLowMemory();
}


}

Answer
List<android.location.Address> addressList= null;

and then u

android.location.Address address = addressList.get(0);

try Log for checking adding location

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

becouse this may not give u any record, and with addressList.get(0) u take the position 0 as u know in java positions start from 0,1,2 and if not have the position 0 u will get error BoundOfException