这是我选择按钮时初始化的代码:
// activate the view record button
Button viewRecordsDB = (Button) findViewById(R.id.BtnViewRecordsDB);
// register the click event with the add record button
viewRecordsDB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.open();
c = db.getAllRecords();
Toast.makeText(ActualSalesTracker.this, "Loading saved sales",
Toast.LENGTH_SHORT).show();
c.moveToFirst();
while (!c.isAfterLast()) {
DisplayRecord(c);
c.moveToNext();
}
db.close();
c.close();
}
});
}
这是包含显示方法的代码:
public void DisplayRecord(Cursor c) {
Toast.makeText(
this,
"Item name" + c.getString(2) + "\n" + "Profit/Loss"
+ c.getString(6), Toast.LENGTH_SHORT).show();
}
这是我在DbAdapter中获取记录的方法:
public Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_ITEM_NAME, KEY_ITEM_COST, KEY_ITEM_PRICE_VALUE,
KEY_ITEM_POSTAGE, KEY_ACTUAL_PL }, null, null, null, null,
null, null);
}
最后这是我的原木猫......
05-08 13:59:59.390: I/Choreographer(2659): Skipped 106 frames! The application may be doing too much work on its main thread.
05-08 14:00:00.830: D/write database(2659): got here
05-08 14:00:00.900: I/Choreographer(2659): Skipped 36 frames! The application may be doing too much work on its main thread.
05-08 14:00:02.440: I/Choreographer(2659): Skipped 306 frames! The application may be doing too much work on its main thread.
05-08 14:00:06.010: I/Choreographer(2659): Skipped 37 frames! The application may be doing too much work on its main thread.
05-08 14:00:07.630: I/Choreographer(2659): Skipped 40 frames! The application may be doing too much work on its main thread.
05-08 14:00:14.530: D/Inserted(2659): Row added to db
05-08 14:00:16.030: E/CursorWindow(2659): Failed to read row 0, column 6 from a CursorWindow which has 17 rows, 6 columns.
05-08 14:00:16.030: D/AndroidRuntime(2659): Shutting down VM
05-08 14:00:16.030: W/dalvikvm(2659): threadid=1: thread exiting with uncaught exception (group=0xb4a68ba8)
05-08 14:00:16.050: E/AndroidRuntime(2659): FATAL EXCEPTION: main
05-08 14:00:16.050: E/AndroidRuntime(2659): Process: com.example.eventbuilder, PID: 2659
05-08 14:00:16.050: E/AndroidRuntime(2659): java.lang.IllegalStateException: Couldn't read row 0, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.database.CursorWindow.nativeGetString(Native Method)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.database.CursorWindow.getString(CursorWindow.java:434)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-08 14:00:16.050: E/AndroidRuntime(2659): at com.example.eventbuilder.ActualSalesTracker.DisplayRecord(ActualSalesTracker.java:150)
05-08 14:00:16.050: E/AndroidRuntime(2659): at com.example.eventbuilder.ActualSalesTracker$3.onClick(ActualSalesTracker.java:129)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.view.View.performClick(View.java:4438)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.view.View$PerformClick.run(View.java:18422)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.os.Handler.handleCallback(Handler.java:733)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.os.Handler.dispatchMessage(Handler.java:95)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.os.Looper.loop(Looper.java:136)
05-08 14:00:16.050: E/AndroidRuntime(2659): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-08 14:00:16.050: E/AndroidRuntime(2659): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 14:00:16.050: E/AndroidRuntime(2659): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 14:00:16.050: E/AndroidRuntime(2659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 14:00:16.050: E/AndroidRuntime(2659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 14:00:16.050: E/AndroidRuntime(2659): at dalvik.system.NativeStart.main(Native Method)
05-08 14:00:19.160: I/Process(2659): Sending signal. PID: 2659 SIG: 9
05-08 14:00:21.020: D/dalvikvm(2713): GC_FOR_ALLOC freed 60K, 4% free 3153K/3284K, paused 41ms, total 45ms
05-08 14:00:21.330: I/Choreographer(2713): Skipped 73 frames! The application may be doing too much work on its main thread.
05-08 14:00:21.430: D/gralloc_goldfish(2713): Emulator without GPU emulation detected.
答案 0 :(得分:0)
该表包含6列,您尝试访问索引为6的列超出范围。将方法DisplayRecord
更改为:
public void DisplayRecord(Cursor c) {
Toast.makeText(
this,
"Item name" + c.getString(1) + "\n" + "Profit/Loss"
+ c.getString(5), Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
列索引从0开始。getString(6)
尝试检索第7列,但Cursor
只有6列。
答案 2 :(得分:0)
请注意,该索引从零开始,因此如果您有6列,则它们的索引将从0到5.您可以使用以下代码来避免此问题:
c.getString(getColumnIndex(String columnName))