Android:java.lang.IndexOutOfBoundsException:索引9无效,大小为9
我的以下代码有问题:Android:java.lang.IndexOutOfBoundsException:索引9无效,大小为9,android,if-statement,int,indexoutofboundsexception,Android,If Statement,Int,Indexoutofboundsexception,我的以下代码有问题: view.setOnClickListener(new OnClickListener() { @Override public void onClick (View v) { int row = position +1; int listLength = data.size(); HashMap<String,String> nextRow = data.get(position+1);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick (View v) {
int row = position +1;
int listLength = data.size();
HashMap<String,String> nextRow = data.get(position+1);
if (row < listLength ) {
nextRow.put("greyedOut","false");
} else {
System.out.println("HATSIKIDEE!!");
}
notifyDataSetChanged();
System.out.println(row);
System.out.println(listLength);
}
});
总计=9
TOTAL=9Java使用基于零的索引——这意味着在位置0处会有一些东西。这意味着在任何列表中,列表中有0-(n-1)项 你需要改变
HashMap<String,String> nextRow = data.get(position+1);
HashMap nextRow=data.get(位置+1);
到HashMap nextRow=data.get(位置)代码>
因此,您要访问的最大索引是8,这是列表中的第9个元素。
您的阵列如下所示:
[0]-第一个元素
[1] -第二要素
.... 等等 Java使用基于零的索引——这意味着在位置0处会有一些内容。这意味着在任何列表中,列表中有0-(n-1)项
int row = position + 1;
int listLength = data.size();
HashMap<String,String> nextRow = null;
if(row < listLength)
{
nextRow = data.get(row);
}
if(nextRow != null)
{
nextRow.put("greyedOut","false");
notifyDataSetChanged();
}
else
{
System.out.println("HATSIKIDEE!!");
}
System.out.println(row);
System.out.println(listLength);
你需要改变
HashMap<String,String> nextRow = data.get(position+1);
HashMap nextRow=data.get(位置+1);
到
HashMap nextRow=data.get(位置)代码>
因此,您要访问的最大索引是8,这是列表中的第9个元素。
您的阵列如下所示:
[0]-第一个元素
[1] -第二要素
.... 等等 int行=位置+1;
int row = position + 1;
int listLength = data.size();
HashMap<String,String> nextRow = null;
if(row < listLength)
{
nextRow = data.get(row);
}
if(nextRow != null)
{
nextRow.put("greyedOut","false");
notifyDataSetChanged();
}
else
{
System.out.println("HATSIKIDEE!!");
}
System.out.println(row);
System.out.println(listLength);
int listLength=data.size();
HashMap nextRow=null;
if(行<列表长度)
{
nextRow=data.get(行);
}
如果(nextRow!=null)
{
下一步,将“greyedOut”,“false”);
notifyDataSetChanged();
}
其他的
{
System.out.println(“HATSIKIDEE!!”);
}
系统输出打印项次(行);
System.out.println(列表长度);
int行=位置+1;
int listLength=data.size();
HashMap nextRow=null;
if(行<列表长度)
{
nextRow=data.get(行);
}
如果(nextRow!=null)
{
下一步,将“greyedOut”,“false”);
notifyDataSetChanged();
}
其他的
{
System.out.println(“HATSIKIDEE!!”);
}
系统输出打印项次(行);
System.out.println(列表长度);
试试这个:
HashMap<String,String> nextRow = null;
if (position + 1 < listLength)
{
nextRow = data.get(position+1);
}
if (nextRow != null)
{
//whatever it is you are trying to achieve by detecting the next row
}
HashMap nextRow=null;
如果(位置+1<列表长度)
{
nextRow=data.get(位置+1);
}
如果(nextRow!=null)
{
//无论您试图通过检测下一行来实现什么
}
试试这个:
HashMap<String,String> nextRow = null;
if (position + 1 < listLength)
{
nextRow = data.get(position+1);
}
if (nextRow != null)
{
//whatever it is you are trying to achieve by detecting the next row
}
HashMap nextRow=null;
如果(位置+1<列表长度)
{
nextRow=data.get(位置+1);
}
如果(nextRow!=null)
{
//无论您试图通过检测下一行来实现什么
}
nextRow使其下方的行可见并处于活动状态,这就是本语句的全部含义。但因为最后一行下面没有下一行,我希望它停止执行该更改。这就是我将其与数组大小进行比较的原因代码>而不是int行=位置+1代码>数据.get(9)每次都会导致IndexOutOfBoundsException。您会注意到data.get(8)是数组中的最后一个元素。你至少试过我们的想法了吗(两个人在同一时间有完全相同的想法是不可能完全错误的)?发生了什么?已经尝试过同样的结果了。位置9尝试激活下一行,但没有下一行。下一行使其下方的行可见并处于活动状态,这就是此语句的全部含义。但因为最后一行下面没有下一行,我希望它停止执行该更改。这就是我将其与数组大小进行比较的原因代码>而不是int行=位置+1代码>数据.get(9)每次都会导致IndexOutOfBoundsException。您会注意到data.get(8)是数组中的最后一个元素。你至少试过我们的想法了吗(两个人在同一时间有完全相同的想法是不可能完全错误的)?发生了什么?已经尝试过同样的结果了。位置9尝试激活下一行,但没有下一行。请查看我对上面答案的评论。请查看我对上面答案的评论。对不起,Kai,我刚刚尝试了你的代码,但仍然给出相同的结果。当我触到最后一行时出错。嗯。。。。如果你把方法的全部内容改成我的代码,我认为这是不可能的。有没有打印过“HATSIKIDEE!!”?谢谢你的答案,再加上Quitin Balsdon的答案,我找到了答案。我看不出我们的答案有什么不同。什么在我的代码中不起作用?对不起,Kai,我刚刚尝试了你的代码,但它仍然给出相同的结果。当我触到最后一行时出错。嗯。。。。如果你把方法的全部内容改成我的代码,我认为这是不可能的。有没有打印过“HATSIKIDEE!!”?谢谢你的答案,再加上Quitin Balsdon的答案,我找到了答案。我看不出我们的答案有什么不同。我的代码中有什么不起作用?
HashMap<String,String> nextRow = null;
if (position + 1 < listLength)
{
nextRow = data.get(position+1);
}
if (nextRow != null)
{
//whatever it is you are trying to achieve by detecting the next row
}