gpt4 book ai didi

Android android.database.CursorIndexOutOfBoundsException : Index x requested, 大小为 x

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:47 28 4
gpt4 key购买 nike

我正在尝试用我的 HighscoreDB 中的数据填充 10 个 TextView 。每次我的应用程序的游戏结束时,都会要求用户将他们的分数连同姓名一起存储在数据库中。然后,我填充 10 个 TextView (实际上是 20 个,但有 10 行带有名称和分数。)。这曾经有效(当 HighscoreDB 中有一些高分记录时),但是自从我创建了一个新的模拟器并因此删除了所有高分行后,我一直在获取“android.database.CursorIndexOutOfBoundsException:请求索引 x,大小为 x”错误。x 表示我当前有多少行,例如,如果我在数据库中有 5 个高分行,则错误将是“请求索引 5,大小为 5”(通过尝试解决我的问题的新方法发现)但它仍然存在,我注意到该指数也增加了)。谁能指出我做错了什么或如何告诉我如何修复我的代码?

这是我的 HighscoreActivity 中的代码,其中将高分放在 TextView 中:

                   db = (new HighscoreDB(this)).getWritableDatabase();
cursor = db.rawQuery("SELECT _id, name, score FROM highscore ORDER BY score DESC", null);
cursor.moveToFirst();
for (int i = 0; i < 10; i++) {



TableRow row = (TableRow)mHighscoreTable.getChildAt(i+1);
TextView tvName = (TextView)row.getChildAt(1);

tvName.setText(cursor.getString(1));
TextView tvScore = (TextView)row.getChildAt(2);

tvScore.setText(Integer.toString(cursor.getInt(2)));


cursor.moveToNext();



}

“for (int i = 0; i < 10; i++) {”这一行是因为我只想在 10 行 TextView 中一次显示 10 个高分。

表格是这样制作的:

private static final String SCORE_TABLE_CREATE = "CREATE TABLE "
+ SCORE_TABLE_NAME
+ " (_id INTEGER PRIMARY KEY autoincrement, "
+ "name TEXT NOT NULL, score INTEGER NOT NULL)";

这是我最近的错误:

05-25 17:03:16.786: E/AndroidRuntime(780): FATAL EXCEPTION: main
05-25 17:03:16.786: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.michiel.test/be.michiel.test.HighscoreActivity}: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:123)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-25 17:03:16.786: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:03:16.786: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:507)
05-25 17:03:16.786: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-25 17:03:16.786: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-25 17:03:16.786: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
05-25 17:03:16.786: E/AndroidRuntime(780): Caused by: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
05-25 17:03:16.786: E/AndroidRuntime(780): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-25 17:03:16.786: E/AndroidRuntime(780): at be.michiel.test.HighscoreActivity.onCreate(HighscoreActivity.java:79)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-25 17:03:16.786: E/AndroidRuntime(780): ... 11 more

请帮我找出我的错误。很长一段时间以来,我一直在努力寻找答案。提前致谢!

最佳答案

如果没有检查下一条记录是否可用,你永远不应该这样做。尝试像下面这样更改您的代码:

db = (new HighscoreDB(this)).getWritableDatabase();
cur = db.rawQuery("SELECT _id, name, score FROM highscore ORDER BY score DESC", null);
int noOfScorer=0;
cur.moveToFirst();
while ((!cur.isAfterLast())&&noOfScorer<11)
{
noOfScorer++;
TableRow row = (TableRow)mHighscoreTable.getChildAt(noOfScorer);
TextView tvName = (TextView)row.getChildAt(1);
TextView tvScore = (TextView)row.getChildAt(2);

tvName.setText(cur.getString(1));
tvScore.setText(Integer.toString(cur.getInt(2)));

cur.moveToNext();
}

希望对你有帮助。

关于Android android.database.CursorIndexOutOfBoundsException : Index x requested, 大小为 x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759069/

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