Android Cursor崩溃

时间:2012-12-20 19:05:06

标签: android cursor

我搜索了网络和stackoverflow但无法找到我需要的解决方案。在我的应用程序中,我将加速度计坐标值记录到数据库中,然后当用户点击按钮时,它将其写入文件并为此坐标值生成一些特征(如标准偏差,平均值,最大值,最小值等)。给定时间间隔)最后,它也将此功能写入文件。但是,当我试图生成这些功能并将其写入文件时,它就会崩溃并将其解析出来。

Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
FATAL EXCEPTION: main
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetDouble(Native Method)
    at android.database.CursorWindow.getDouble(CursorWindow.java:543)
    at android.database.CursorWindow.getFloat(CursorWindow.java:594)
    at android.database.AbstractWindowedCursor.getFloat(AbstractWindowedCursor.java:81)
    at com.example.coordinates.MainActivity.generateFeatures(MainActivity.java:259)
    at com.example.coordinates.MainActivity.dosyayaYaz(MainActivity.java:239)
    at com.example.coordinates.MainActivity.onDialogPositiveClick(MainActivity.java:668)
    at com.example.coordinates.dialogFragement$1.onClick(dialogFragement.java:54)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4898)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    at dalvik.system.NativeStart.main(Native  Method)

这是我认为我得到错误的地方。

private void generateFeatures(String label){

        boolean bitti=false;

        Cursor firstTimeHandler=db.rawQuery("SELECT MIN(zaman) FROM Koordinatlar", null);
        firstTimeHandler.moveToFirst();

        float firstTime= firstTimeHandler.getFloat(firstTimeHandler.getColumnIndex("zaman"));
        float nextTime=firstTime+3000;

        Cursor lastTimeHandler=db.rawQuery("SELECT MAX(zaman) FROM Koordinatlar", null);
        lastTimeHandler.moveToFirst();

        float lastTime=lastTimeHandler.getFloat(firstTimeHandler.getColumnIndex("zaman"));



        while(!bitti){

            calculateAverage(firstTime,nextTime);
            calculateStandartDev(firstTime,nextTime);
            calculateAverageAbsoluteDifferance(firstTime,nextTime);
            calculateAverageResultant(firstTime,nextTime);
            calculateMax(firstTime,nextTime);

            firstTime=nextTime;
            nextTime=nextTime+3000;

            if(nextTime>=lastTime){
                nextTime=lastTime;

                calculateAverage(firstTime,nextTime);
                calculateStandartDev(firstTime,nextTime);
                calculateAverageAbsoluteDifferance(firstTime,nextTime);
                calculateAverageResultant(firstTime,nextTime);
                calculateMax(firstTime,nextTime);

                bitti=true;

            }

        }

        dosyayaYaz2(label);
    }

2 个答案:

答案 0 :(得分:1)

我建议使用cursor.getFloat(0),因为你的光标中只有一列,而其他人已经说过MIN(zaman)的命名很棘手

答案 1 :(得分:0)

你的日志说错了 Couldn't read row 0, col -1 from CursorWindow.

基本上,它试图访问一个不存在的字段。 这是由这里的行引起的:或/和firstTime声明。

 float lastTime=lastTimeHandler.getFloat(firstTimeHandler.getColumnIndex("zaman"));

所以基本上字段zaman不存在。

但确实如此?

检查案例。出于某种奇怪的原因,其区分大小写。 您的列可能名为 Zaman ,而不是 zamon

为什么sql不区分大小写,这个获取列索引的android api不是。