English 中文(简体)
Cursor Index out of BoundsExpurvision: 请求索引xx, 大小为x
原标题:Android android.database.CursorIndexOutOfBoundsException: Index x requested, with a size of x

I m trying to fill up 10 textviews with data from my HighscoreDB. Every time my application s game ends, the user is asked to store their score in the database, along with a name. Then, I fill 10 textviews (actually 20, but there are 10 rows with name and score.). This used to work (when there were a few highscore records in HighscoreDB), but ever since I created a new emulator and thereby deleting all the highscore rows, I keep getting the "android.database.CursorIndexOutOfBoundsException: Index x requested, with a size of x" error. The x represents how many rows I currently have, for instance if I have 5 highscore rows in the DB, the error will be "Index 5 requested, with a size of 5" (found that out by trying out new ways to fix my problem but it remained, and I noticed that the index also increases). Can anyone point out what I m doing wrong or how tell me how to fix my code?

这是我高分动作中的代码 高分人物被放入文字视图中

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



    }

“ i = 0; i & lt; 10; i++) { ” 的行是因为我想在文本视图的 10 行中一次显示 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

Please help me find my mistake. I ve been trying to find out for a long time now. Thanks in advance!

最佳回答

您永远不应该在不检查下一张记录是否可用的情况下这样做。 请尝试修改您的代码, 如下面的代码 :

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

希望这能帮到你

问题回答

您获取错误, 原因是您正在尝试访问第 11 张表格位子, 而这个位子并不存在 。

Replace
TableRow row = (TableRow)mHighscoreTable.getChildAt(i+1);
with
TableRow row = (TableRow)mHighscoreTable.getChildAt(i);


Also, your indexes are getting bigger because you set the ID property to "auto increment".

代码的另一个问题是您重新拨打光标. move ToNext (); 在第 10 项上 。 您可以修改您的“ for” 语句, 这样看起来 :

for (int i = 0; i < 10 && !cursor.isAfterLast(); i++) {




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签