java.lang.IndexOutOfBoundsException:索引0无效,大小为0

时间:2016-12-15 11:12:31

标签: android google-maps

这是我的代码和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();
    }

我的logcat和错误显示在这里地址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.

1 个答案:

答案 0 :(得分:0)

来自Geocoder.getFromLocation)()的{​​{3}}

  

返回

     

列出地址对象列表。 如果未找到匹配项或没有可用的后端服务,则返回null或空列表。

在你的例子中,它返回一个空的List,你必须检查这种情况:

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