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!