简体   繁体   中英

Android Cursor crash

i've searched on web and stackoverflow but couldnt find the solution i need. In my application, i record the accelerometer coordinate values to a database, and then when user clicks on the button it writes it to a file and generate some features for this coordinate values (like standart deviation,average,max,min etc. for the given time interval) finally, it writes this features to a file too. But when i ever tried to generate these features and write it to a file, it crashed and gave that out.

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)

this is where i think i get error.

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);
    }

我建议使用cursor.getFloat(0),因为您的光标中只有一列,而其他人则指出MIN(zaman)的命名很棘手

Your log says whats wrong Couldn't read row 0, col -1 from CursorWindow.

Basically, it is trying to access a field that doesnt exist. This is caused by the line here: or/and firstTime declaration.

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

So basically the field zaman doesnt exist.

But it does?

Check the case. Its case sensitive for some bizaree reason. It is likely your column is named Zaman not zamon

Why sql is case insensitive, this android api to get the column index, is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM