java - 当我要求它在 toast 中显示保存的数据时,我的应用程序崩溃了 - 我

标签 java android sqlite

这是当我选择按钮时初始化的代码:

// 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.

最佳答案

该表包含 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();
    }

关于java - 当我要求它在 toast 中显示保存的数据时,我的应用程序崩溃了 - 我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23549476/

相关文章:

java - Android,如何将 LoaderManager 和 Loader 与 sqldatabase 结合使用

Java 使用 Math.ceil 将整数四舍五入

android: ConnectivityManager.CONNECTIVITY_ACTION,网络连接状态未改变时发送广播

android - 将底部重力设置为单个 View Android

sql - 将字符串添加到我的数据库行之一 - 为什么这不起作用?

java - 获取在封闭范围内定义的局部变量必须是最终的或有效的最终

java - 我可以使用 java 创建 Windows Phone 应用程序吗

android - 多个共享元素

arrays - 在 SQLite 中存储在另一个表中引用的数组

android - 如何在 SQLite 中获取每日总金额