我有这段代码,它提供了Exception
:
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);
}
这里我添加了数据
if(activestatus){////////////activation
String taxinos= tokens.nextToken();
StringTokenizer tokentaxi = new StringTokenizer(taxinos, ",");
int taxicount = tokentaxi.countTokens();
if(taxicount>0){
taxicount = BackgroundService.taxis.size();///////addded to combat error prone value
int jj=0;
for(int i=0;i<taxicount;i++){
String tax = tokentaxi.nextToken();
StringTokenizer tok = new StringTokenizer(tax, ";");
int taxidetailcount = tok.countTokens();
if(taxidetailcount==5){
if(tok.hasMoreTokens())
BackgroundService.taxis.set(jj,tok.nextToken());//was add without index
if(tok.hasMoreTokens())
BackgroundService.taxitype.set(jj,tok.nextToken());
if(tok.hasMoreTokens())
BackgroundService.taximoving.set(jj,Boolean.parseBoolean(tok.nextToken().toLowerCase()));//////////added new
if(tok.hasMoreTokens())
BackgroundService.ppanic.set(jj,Boolean.parseBoolean(tok.nextToken().toLowerCase()));//////////added new
if(tok.hasMoreTokens())
BackgroundService.ppower.set(jj,Boolean.parseBoolean(tok.nextToken().toLowerCase()));//////////added new
}
else{
Log.e("Service", "got taxi less value="+i+" ->"+taxidetailcount);
}
Log.w("Service", "got taxi "+i+" ->"+tax);
}
我有一个后台服务,其中所有这些都已初始化
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>();
答案 0 :(得分:0)
你的集合是空的,因为应该填充数据的循环没有运行,那是因为你在这里覆盖了循环计数器
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