Android IndexOutOfBounds:索引0无效,大小为0

Android IndexOutOfBounds:索引0无效,大小为0,android,Android,我有这个代码,它给出了例外情况: IndexOutOfBound :Invalid Index 0, Size is 0 这是我的密码: ArrayList<HashMap<String, String>> vehlist; HashMap<String, String> vehmap; for(int i = 0;i<BackgroundService.taxis.size();i++){ vehmap = n

我有这个代码,它给出了
例外情况

IndexOutOfBound :Invalid Index 0, Size is 0
这是我的密码:

 ArrayList<HashMap<String, String>> vehlist;
 HashMap<String, String> vehmap;

 for(int i = 0;i<BackgroundService.taxis.size();i++){
                vehmap = new HashMap<String, String >();
                vehmap.put("vehno", BackgroundService.taxis.get(i));
                vehmap.put("vehmotion",BackgroundService.taximoving.get(i)+"");
                vehmap.put("vehpanic", BackgroundService.ppanic.get(i)+"");
                vehmap.put("vehpower", BackgroundService.ppower.get(i)+"");
                vehlist.add(vehmap);
            }
arraylistvehlist;
HashMap-vehmap;
对于(int i=0;i0){
taxicount=BackgroundService.taxis.size();//added以消除容易出错的值
int jj=0;

对于(int i=0;i您的集合为空,因为应该填充数据的循环没有运行,这是因为您在此处覆盖了循环计数器

 if(taxicount > 0) { // Say, taxicount is 10
        taxicount = BackgroundService.taxis.size();
        // Now, taxicount is 0
        int jj = 0;
        for(int i=0; i < taxicount; i++) { // data loop won't run now
if(taxicount>0){//比如说,taxicount是10
taxicount=BackgroundService.taxis.size();
//现在,出租车计数是0
int jj=0;
对于(inti=0;i
您的一个集合:taximoving、ppanic、ppower未初始化或不包含任何元素。您确定所有这些集合的大小都相同吗?发布完整的代码,我认为您没有正确初始化集合。所有这些集合都在另一个Java文件(即BackgroundService)中初始化,但您在哪里向它们添加数据?我遇到异常在vehmap.put(“vehpanic”,BackgroundService.ppanic.get(i)+”);vehmap.put(“vehpower,BackgroundService.ppower.get(i)+”);vehlist.add(vehmap);}中,请选择任何好的Java书籍并浏览List.size()、add()和set()。您将开始理解这里的问题。在设置()或从中获取()之前,列表中必须有某些内容。
 static List<String> taxis,taxitype,taxifull;
 static List<Boolean> taximoving,ppanic,ppower;
 //Messenger mclients = null; 
  ArrayList<Messenger> mClients = new ArrayList<Messenger>(); 



    taxis = new ArrayList<String>();
    taxitype = new ArrayList<String>(); 
    taxifull = new ArrayList<String>();
    taximoving = new ArrayList<Boolean>();
    ppanic = new ArrayList<Boolean>();
    ppower = new ArrayList<Boolean>();
 if(taxicount > 0) { // Say, taxicount is 10
        taxicount = BackgroundService.taxis.size();
        // Now, taxicount is 0
        int jj = 0;
        for(int i=0; i < taxicount; i++) { // data loop won't run now