发现索引0,android中的大小为0

时间:2015-02-26 13:30:09

标签: android

“我发现索引超出约束异常我该如何解决?”

public static void addToCart() {
ArrayList<ItemData> iArr;
if (holdSelection != null) {
String categoryID = holdSelection.getCategoryID();
int position = categoriesAddedd.indexOf(holdSelection.getCategoryID());
int itemPosition =categoriesitemAddedd.indexOf(holdSelection.getItemData().get(0).getItemID());
if (!categoriesAddedd.contains(categoryID)) {
    CategoryData data = new CategoryData();
            data.setCategoryID(categoryID);
            data.setCategoryName(holdSelection.getCategoryName());
            data.setItemData(holdSelection.getItemData());
            mAddedToCart.add(data);
            categoriesAddedd.add(categoryID);   categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext,R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
} else {
if (position>=0) {
if(itemPosition>=0){
if(holdSelection.getItemData().get(0).getScaled()){
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}
}else{
try{
CategoryData data = mAddedToCart.get(position);             
                     iArr = data.getItemData();
                     iArr.add(holdSelection.getItemData().get(0));                  
                    categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext, R.string.Add_to_cart,Toast.LENGTH_SHORT).show();                       
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(mContext, "There is " +e,Toast.LENGTH_SHORT).show();                     
System.out.println("Exception : "+ e.getMessage());
e.printStackTrace();                
}       
}
}
}
}
}

1 个答案:

答案 0 :(得分:1)

如果没有最少的(结构化的)信息,也无法提供帮助(如果您愿意的话,也可以没有Hello,请你,礼貌的话)。

首先,您必须使用括号在发布时显示代码,因此您的干净代码应如下所示:

public static void addToCart() { 
ArrayList iArr; 

    if (holdSelection != null) { 
    String categoryID = holdSelection.getCategoryID(); 
    int position = categoriesAddedd.indexOf(holdSelection.getCategoryID()); 
    int itemPosition =categoriesitemAddedd.indexOf(holdSelection.getItemData().get(0).getItemID()); 

        if (!categoriesAddedd.contains(categoryID)) { 
            CategoryData data = new CategoryData(); 
            data.setCategoryID(categoryID); 
            data.setCategoryName(holdSelection.getCategoryName()); 
            data.setItemData(holdSelection.getItemData()); 
            mAddedToCart.add(data); 
            categoriesAddedd.add(categoryID); 
            categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID()); 
            Toast.makeText(mContext,R.string.Add_to_cart,Toast.LENGTH_SHORT).show(); 
        } else { 
            if (position>=0) { 
                if(itemPosition>=0){ 
                    if(holdSelection.getItemData().get(0).getScaled()){ 
                        Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show(); 
                    }else{ 
                        Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show(); 
                    } 
                }else{ 
                    try{ 
                        CategoryData data = mAddedToCart.get(position);
                        iArr = data.getItemData(); iArr.add(holdSelection.getItemData().get(0));
                        categoriesAddedd.add(categoryID); 
                        categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID()); 
                        Toast.makeText(mContext, R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
                    }
                    catch (Exception e) 
                    { // TODO: handle exception Toast.makeText(mContext, "There is " +e,Toast.LENGTH_SHORT).show();
                        System.out.println("Exception : "+ e.getMessage()); e.printStackTrace();
                    }
                } 
            } 
        }       
    } 
}

然后我的第一个问题是:什么是holdSelection有关? 请添加代码的更多部分作为日志(Remees询问)。

亚历。