gpt4 book ai didi

java - 获取 "CursorIndexOutOfBoundsException "

转载 作者:行者123 更新时间:2023-12-01 13:40:22 25 4
gpt4 key购买 nike

我正在尝试检索sqlite数据库的数据。并将其放在字符串上以进一步显示,但我收到此错误,请帮助我解决该错误!

这是显示错误的 logcat 数据

    12-31 13:18:55.141: E/1.6(3833): 2222
12-31 13:18:55.191: E/1.2.2(3833): android.database.CursorIndexOutOfBoundsException: Index 9 requested, with a size of 9
12-31 13:18:55.191: E/1.7(3833): 2222

这是显示错误的代码段!

public String getdata() {
// TODO Auto-generated method stub
Log.e("1.1", "2222");
String[] column = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Log.e("1.2", "2222");
Cursor c = ourDatabase.query(DATABASE_TABLE, column, null, null, null, null, null);
Log.e("1.3", "2222");
String result = "";
Log.e("1.4", "2222");

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_HOTNESS);
Log.e("1.5", "2222");

try{
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext());
{
Log.e("1.6", "2222");

result = result + c.getString(0) + " " + c.getString(1) + " " + c.getString(2) + "\n";
}
}catch(Exception e)
{

Log.e("1.2.2", e.toString());
}

Log.e("1.7", "2222");
return result;
}

注意:将以下代码放入 try catch 后,它会移至下一个 Activity ,但不会显示任何结果数据

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext());
{
Log.e("1.6", "2222");

result = result + c.getString(0) + " " + c.getString(1) + " " + c.getString(2) + "\n";
}

下一个 Activity 代码:

TextView tv = (TextView) findViewById(R.id.textView3);

Log.e("1", "111111");
HotOrNot info = new HotOrNot(this);
Log.e("2", "111111");
info.open();
Log.e("3", "111111");
String data = info.getdata();
Log.e("4", "111111");
info.close();
Log.e("5", "111111");
tv.setText(data);

Log.e("6", "111111");
}

}

最佳答案

你的分号太多了:

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()); // look here :(

...因此您的 for 循环运行到光标末尾。然后,您尝试从光标获取一些数据,但失败了,因为您已经在最后一个条目之后了。

就用这个:

while(c != null && c.moveToNext())
{
Log.e("1.6", "2222");

result = result + c.getString(0) + " "
+ c.getString(1) + " "
+ c.getString(2) + "\n";
}

您很可能可以跳过c != null

关于java - 获取 "CursorIndexOutOfBoundsException ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com