SQLite中字段槽的错误请求

时间:2011-12-31 04:32:22

标签: android sqlite android-layout

当我尝试恢复已放入SQLite数据库的信息时,我遇到了问题。我得到了一个成功的消息,它已被放入,但当我开始我的活动拉动数据时它会崩溃。

logcat的

12-30 22:14:13.005: D/dalvikvm(275): GC_EXTERNAL_ALLOC freed 734 objects / 54696 bytes in 92ms
12-30 22:19:00.275: W/KeyCharacterMap(275): No keyboard for id 0
12-30 22:19:00.275: W/KeyCharacterMap(275): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 22:19:11.185: D/dalvikvm(275): GC_FOR_MALLOC freed 7425 objects / 301256 bytes in 70ms
12-30 22:19:11.257: E/CursorWindow(275): Bad request for field slot 0,-1. numRows = 8, numColumns = 4
12-30 22:19:11.265: D/AndroidRuntime(275): Shutting down VM
12-30 22:19:11.265: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-30 22:19:11.285: E/AndroidRuntime(275): FATAL EXCEPTION: main
12-30 22:19:11.285: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.walkarc.babytracker/com.walkarc.babytracker.BathReport}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.os.Looper.loop(Looper.java:123)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 22:19:11.285: E/AndroidRuntime(275):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 22:19:11.285: E/AndroidRuntime(275):  at java.lang.reflect.Method.invoke(Method.java:521)
12-30 22:19:11.285: E/AndroidRuntime(275):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 22:19:11.285: E/AndroidRuntime(275):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 22:19:11.285: E/AndroidRuntime(275):  at dalvik.system.NativeStart.main(Native Method)
12-30 22:19:11.285: E/AndroidRuntime(275): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.database.CursorWindow.getString_native(Native Method)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.database.CursorWindow.getString(CursorWindow.java:329)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
12-30 22:19:11.285: E/AndroidRuntime(275):  at com.walkarc.babytracker.MyDBAdapter.getData(MyDBAdapter.java:91)
12-30 22:19:11.285: E/AndroidRuntime(275):  at com.walkarc.babytracker.BathReport.onCreate(BathReport.java:17)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-30 22:19:11.285: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我的录入代码

public long createEntry(String date, String time, String hairWash,
            String comment) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_BATHDATE, date);
        cv.put(KEY_BATHTIME, time);
        cv.put(KEY_HAIRWASH, hairWash);
        cv.put(KEY_COMMENTS, comment);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

我查看db retrive活动的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Date" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Time" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Hair Wash?" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Comments" />
"

        </TableRow>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="get info from db" />
    </TableLayout>

</LinearLayout>

Java for it

    public class BathReport extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bathreport);
        TextView tv = (TextView) findViewById(R.id.textView3);
        MyDBAdapter info = new MyDBAdapter(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);
    }


public String getData() {
        String[] columns = new String[] { KEY_BATHDATE, KEY_BATHTIME,
                KEY_HAIRWASH, KEY_COMMENTS };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";
        int iRow = c.getColumnIndex(KEY_ROWID);
        int iDate = c.getColumnIndex(KEY_BATHDATE);
        int iTime = c.getColumnIndex(KEY_BATHTIME);
        int iHair = c.getColumnIndex(KEY_HAIRWASH);
        int iCom = c.getColumnIndex(KEY_COMMENTS);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iRow) + " " + c.getString(iDate)
                    + " " + c.getString(iTime) + " " + c.getString(iHair) + " "
                    + c.getString(iCom) + "\n";
        }

        return result;
    }

任何人都知道为什么会出错?

1 个答案:

答案 0 :(得分:1)

好的,试试

public String getData() {
    String[] columns = new String[] { KEY_BATHDATE, KEY_BATHTIME,
            KEY_HAIRWASH, KEY_COMMENTS };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
            null, null);
    if (c != null && c.moveToFirst()) {
    int iRow = c.getString(0);
    int iDate = c.getString(1);
    int iTime = c.getString(2);
    int iHair = c.getString(3);
    int iCom = c.getString(4);

    String[] rData = { iRow, iDate, iTime, iHair, iCom };

    return rData;

    }

    return null;
}

修改

String[] columns = new String[] { KEY_BATHDATE, KEY_BATHTIME,
            KEY_HAIRWASH, KEY_COMMENTS };

是4个结果,但是

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iDate = c.getColumnIndex(KEY_BATHDATE);
    int iTime = c.getColumnIndex(KEY_BATHTIME);
    int iHair = c.getColumnIndex(KEY_HAIRWASH);
    int iCom = c.getColumnIndex(KEY_COMMENTS);

正在寻找5个结果。添加你的_id列

String[] columns = new String[] { KEY_ROWID, KEY_BATHDATE, KEY_BATHTIME,
            KEY_HAIRWASH, KEY_COMMENTS };