Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Here it's my code and logcat!!!!

public void onMapReady(GoogleMap googleMap) {

 mMap = googleMap;

 mMap.setOnMapClickListener(this);

 mMap.setMyLocationEnabled(true);

mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

 mMap.setMyLocationEnabled(true);

mLocationRequest = new LocationRequest();

 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

 mLocationRequest.setInterval(2000);

 mLocationRequest.setFastestInterval(1000);

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

 String provider = locationManager.getBestProvider(new Criteria(), true);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling

        return;
    }
    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null) {
        Log.e("TAG", "GPS is on");
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Toast.makeText(getApplicationContext()
                , "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();

    } else {

        locationManager.requestLocationUpdates(provider, 4000, 0,  this);
    }
    LatLng HYDERABAD = new LatLng(latitude, longitude);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HYDERABAD, 12));
    try {
        List<Address> addresses;
        Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.ENGLISH);
        addresses = geocoder.getFromLocation(latitude, longitude, 1);

        if (Geocoder.isPresent()) {
            Toast.makeText(getApplicationContext(), "geocoder present",
                    Toast.LENGTH_SHORT).show();

          Address returnAddress = addresses.get(0);
           Log.d("LIne ", returnAddress.toString());
            String localityString = returnAddress.getAddressLine(2);
           Log.d("millatary ", localityString);

            str.append(localityString).append(" ");

            marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title(
                    str.toString());
            etOrigin.setText(str.toString());

            mCurrLocationMarker = mMap.addMarker(marker);
            Toast.makeText(getApplicationContext(), str,
                    Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(getApplicationContext(),
                    "geocoder not present", Toast.LENGTH_SHORT).show();

    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Exception",
                Toast.LENGTH_SHORT).show();
    }

its my logcat and error showing here Address returnAddress = addresses.get(0);

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.swetha.pc.barcoderead.tools.MapsActivity.onMapReady(MapsActivity.java:389)
  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:5273)
  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:903)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Application terminated.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

From the documentation of Geocoder.getFromLocation)()

Returns

List a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.

In your example it's returning an empty List and you must check for this case:

if (addresses != null && !addresses.isEmpty()) {
    // Your code:
    Address returnAddress = addresses.get(0);
    Log.d("LIne ", returnAddress.toString());
    String localityString = returnAddress.getAddressLine(2);
    Log.d("millatary ", localityString);

    str.append(localityString).append(" ");

    marker = new MarkerOptions().position(
        new LatLng(latitude, longitude)).title(
                str.toString());
    etOrigin.setText(str.toString());

    mCurrLocationMarker = mMap.addMarker(marker);
    Toast.makeText(getApplicationContext(), str,
        Toast.LENGTH_SHORT).show();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...