IndexOutOfBoundsException

IndexOutOfBoundsException

本文介绍了java.lang.IndexOutOfBoundsException:索引0无效,大小为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果(ActivityCompat.checkSelfPermission(此,Manifest.permission.ACCESS_FINE_LOCATION)= PackageManager.PERMISSION_GRANTED&安培;!&安培;!ActivityCompat.checkSelfPermission(此,Manifest.permission.ACCESS_COARSE_LOCATION)= PackageManager.PERMISSION_GRANTED){
// TODO:考虑调用

return;
}
位置位置= 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 =新LatLng(纬度,经度);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HYDERABAD,12));
尝试{
列表<地址>地址;
Geocoder geocoder =新的地理编码器(MapsActivity.this,Locale.ENGLISH);
addresses = geocoder.getFromLocation(latitude,longitude,1);
$ b $ if(Geocoder.isPresent()){
Toast.makeText(getApplicationContext(),geocoder present,
Toast.LENGTH_SHORT).show();

地址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();
$ b $ catch(IOException e){
Toast.makeText(getApplicationContext(),Exception,
Toast.LENGTH_SHORT).show();

$ / code>

我的logcat和error显示在这里Address returnAddress = addresses.get(0) ;

  java.lang.IndexOutOfBoundsException:无效索引0,大小为0 
。在java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:255)在java.util.ArrayList.get(ArrayList.java:308)
在com.swetha.pc.barcoderead.tools.MapsActivity.onMapReady
(MapsActivity.java:389 )
在com.google.android.gms.maps.SupportMapFragment $ zza $ 1.zza(未知来源)
在com.google.android.gms.maps.internal.zzt $ zza.onTransact(未知源)
at android.os.Binder.transact(Binder.java:380)
at zu.a(:com.google.android.gms.DynamiteModulesB:82)
at maps。 ad.t $ 5.run(未知源代码)
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:527 3)
在java.lang.reflect.Method.invoke(本地方法)
在java.lang.reflect.Method.invoke(Method.java:372)
at com.android。 internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
应用
终止。


解决方案

From ) ()

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

这篇关于java.lang.IndexOutOfBoundsException:索引0无效,大小为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 20:42