Java 在android中获取indexoutofbounds异常

Java 在android中获取indexoutofbounds异常,java,android,Java,Android,删除职位时,我得到了一个ArrayIndexOutOfBoundsException,因为职位从1开始,但数组索引从0开始。你能解释一下如何解决这个问题吗 if (isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)) { count = count + 1; arrActivation.add(childPosition); context.getSummaryFragment().

删除职位时,我得到了一个
ArrayIndexOutOfBoundsException
,因为职位从1开始,但数组索引从0开始。你能解释一下如何解决这个问题吗

if (isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE))
{
    count = count + 1;
    arrActivation.add(childPosition);
    context.getSummaryFragment().getOli(arrActivation,groupPosition);
    if (!context.getSummaryFragment().activateSystem.isEnabled())
    {
        context.getSummaryFragment().enableButton(true);
    }
}
else if (!isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE))
{
    count = count - 1;
    arrActivation.remove(childPosition);
}

context.getSummaryFragment().getOli(arrActivation,groupPosition);

if (context.getSummaryFragment().activateSystem.isEnabled() && count <= 0)
{
    context.getSummaryFragment().enableButton(false);
}
}
if(isChecked&&groupName.equals(OLIConstants.FAILED\u TO\u ACTIVATE))
{
计数=计数+1;
ArraActivation.add(子位置);
getSummaryFragment().getOli(ArraActivation,groupPosition);
如果(!context.getSummaryFragment().activateSystem.isEnabled())
{
context.getSummaryFragment().enableButton(true);
}
}
如果(!isChecked&&groupName.equals(OLIConstants.FAILED_TO_ACTIVATE))则为else
{
计数=计数-1;
阵列激活。移除(子位置);
}
getSummaryFragment().getOli(ArraActivation,groupPosition);

如果(context.getSummaryFragment().activateSystem.isEnabled()&&count您可以使用
arrActivation.add(childPosition-1);


希望这能解决您的问题。

我将根据您的代码做一个假设:

  • childPosition是要存储在ArrayList ArraActivation中的数据

    arrActivation.add(childPosition);

  • childPosition是一个整数(因为您得到的数组索引超出了绑定异常)

    arrActivation.remove(childPosition);

  • 现在,如果您尝试添加一个整数并使用相同的参数将其删除,则可能会出现AIOOB异常。请看:

        ArrayList here = new ArrayList();
        here.add(5);
        // The following line will exception because
        // The length of the array list is 1 at this point
        here.remove(5);
    
    但这将起作用:

        ArrayList here = new ArrayList();
        here.add(5);
        // The following line will not exception 
        here.remove(Integer.valueOf(5));
    
    因为现在我们使用的是remove(Object o)(它查找带有Object o的数据,并删除第一次出现的数据)函数,而不是remove(int index)(它尝试删除arraylist的索引)函数


    我希望这能有所帮助:)

    为什么不能将count初始化为比实际值大一个整数呢?示例代码既不显示count初始化,也不显示任何循环或数组对象。请发布更多代码