2016-12-15 163 views
-1

這裏是我的代碼和logcat !!!!java.lang.IndexOutOfBoundsException:索引0無效,大小爲0

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和錯誤在這裏顯示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. 

回答

0

Geocoder.getFromLocation)()

返回

名單地址對象的列表the documentation如果找不到匹配或沒有後端服務可用,則返回空列表或空列表。

在您的示例它返回一個空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(); 
} 
相關問題