当前位置: 首页 > 知识库问答 >
问题:

Androidandroid.database.CursorIndexOutOfBoundsExcture:索引x请求,大小为x

戚育
2023-03-14

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

这是我的高分活动中的代码,其中高分放在文本视图中:

                   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

这是表格的制作方法:

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

请帮我找出我的错误。我一直在寻找答案。提前谢谢!

共有2个答案

江建明
2023-03-14

您会出现错误,因为您试图访问不存在的第11个TableRow子节点。


TableRow row=(TableRow)mHighscoreTable.getChildAt(i 1);
with
TableRow row=(TableRow)mHighscoreTable.getChildAt(i);
因为您将ID属性设置为“自动增量”而变得越来越大。

代码的另一个问题是调用cursor.moveToNext();关于第10项。您可以修改“for”语句,使其如下所示:

for (int i = 0; i < 10 && !cursor.isAfterLast(); i++) {
长孙兴德
2023-03-14

如果不检查下一条记录是否可用,则决不能执行此操作。尝试更改代码,如下所示:

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();
    }

希望这对你有帮助。

 类似资料: