有一个ava.lang.IndexOutOfBoundsException:索引0无效,大小是0错误?

时间:2017-01-11 11:14:33

标签: java android arraylist indexoutofboundsexception

我的res / strings中有一个字符串数组。 XML。我只是试图遍历它们并为谷歌地图添加一个标记但我在我的阵列上得到一个OutOfBoundsException。这是错误...

             Process: com.gerhorgan.propertyprice, PID: 15147
             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.gerhorgan.propertyprice.MapsActivity.onMapReady(MapsActivity.java:83)
             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:5910)
             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:1405)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

这是我的代码..

            package com.gerhorgan.propertyprice;

            import android.location.Address;
            import android.location.Geocoder;
            import android.os.Bundle;
            import android.support.v4.app.FragmentActivity;
            import android.view.View;
            import android.widget.ZoomControls;

            import com.google.android.gms.maps.CameraUpdateFactory;
            import com.google.android.gms.maps.GoogleMap;
            import com.google.android.gms.maps.OnMapReadyCallback;
            import com.google.android.gms.maps.SupportMapFragment;
            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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {

                private GoogleMap mMap;
                ZoomControls zoom;
                private String[] addressArray;


                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_maps);
                    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(this);

                    zoom = (ZoomControls) findViewById(R.id.zcZoom);

                    zoom.setOnZoomOutClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mMap.animateCamera(CameraUpdateFactory.zoomOut());

                        }
                    });

                    zoom.setOnZoomInClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            mMap.animateCamera(CameraUpdateFactory.zoomIn());

                        }
                    });

                }


                /**
                 * Manipulates the map once available.
                 * This callback is triggered when the map is ready to be used.
                 * This is where we can add markers or lines, add listeners or move the camera. In this case,
                 * we just add a marker near Sydney, Australia.
                 * If Google Play services is not installed on the device, the user will be prompted to install
                 * it inside the SupportMapFragment. This method will only be triggered once the user has
                 * installed Google Play services and returned to the app.
                 */
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    mMap = googleMap;


                    addressArray= getResources().getStringArray(R.array.addresses);
                    Geocoder geocoder= new Geocoder(MapsActivity.this);
                    List<Address> addressList= null;
                    for(int i=0; i< addressArray.length; i++)

                    {
                        try {
                            addressList= geocoder.getFromLocationName(addressArray[i],1);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Address address= addressList.get(0);
                        LatLng latlng= new LatLng(address.getLatitude(), address.getLongitude());
                        mMap.addMarker(new MarkerOptions().position(latlng).title(addressArray[i]));
                        mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));


                    }






                }
            }

6 个答案:

答案 0 :(得分:2)

您的addressList为空。因此,当您尝试执行addressList.get(0);时,您将尝试获取空List的第一个元素。然后你会得到那个例外。

答案 1 :(得分:1)

你只需要确定数组是否为null并且大小是否大于零所以我认为你只需要这样:

if(addressList != null && addressList.size() > 0) 
{

   Address address= addressList.get(0);
   LatLng latlng= new LatLng(address.getLatitude(), address.getLongitude());
   mMap.addMarker(new MarkerOptions().position(latlng).title(addressArray[i]));
   mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
} 

答案 2 :(得分:0)

您正在访问的数组列表为空,其大小为0

addressList.get(0);

检查数组列表的大小并访问它。

正如vishal所提到的,你需要初始化数组列表。

您还需要使用ArraList.add()将对象添加到数组列表中。

addressList.add();

同时检查您是否在此行中获取值

addressList= geocoder.getFromLocationName(addressArray[i],1);

答案 3 :(得分:0)

在onMapReady方法中,您没有初始化列表。

List<Address> addressList= null;

替换你的代码
List<Address> addressList= new ArrayList<>();

答案 4 :(得分:0)

这段代码给你例外。

Address address= addressList.get(0);

对代码稍做修改即可。

if(!addressList.isEmpty())
   Address address= addressList.get(0);

答案 5 :(得分:0)

addressList在这里是空的。但是尝试从空列表中获取第一个元素,即抛出异常。

为避免这种情况,您可以添加支票

if(addressList.size() > 0) {
 Address address= addressList.get(0);
 //perform remaining
 }

另外,您检查geocoder.getFromLocationName(addressArray [i],1)返回空的原因。