简体   繁体   中英

Android Sqlite cursor initialization error

I have a simple sqlite class which saves the two parameters NAME and BEHAVE of a person with button a button.But the activity crashes when I click the button to save data.Here is my logcat saying- make sure the cursor is initialized properly.

12-01 10:38:05.589    9336-9336/? E/CursorWindow﹕ Failed to read row 0,      column -1 from a CursorWindow which has 8 rows, 3 columns.
12-01 10:38:05.589    9336-9336/? D/AndroidRuntime﹕ Shutting down VM
-01 10:38:05.599    9336-9336/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: androphile.alpha, PID: 9336
java.lang.RuntimeException: Unable to start activity ComponentInfo{androphile.alpha/androphile.alpha.sqlview}: 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.app.ActivityThread.performLaunchActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: 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.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:451)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at androphile.alpha.behave.getData(behave.java:80)
at androphile.alpha.sqlview.onCreate(sqlview.java:24)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)

Here is my Handler class.It also show cursor should be freed up after use.

  public class behave  {
        public static final String KEY_ROWID="_id ";
        public static final String KEY_NAME="persons_name";
        public static final String KEY_BEHAVE="persons_behave";
        private static final String DATABASE_NAME="behaveDB";
        private static final String DATABASE_TABLE="personTable";
        private static final int  DATABASE__VERSION=1;
        private Dbhelper ourHelper;
        private final Context ourContext;
        private SQLiteDatabase ourDatabase;

        private class Dbhelper extends SQLiteOpenHelper{
            public Dbhelper(Context context){
                super(context, DATABASE_NAME, null, DATABASE__VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE "+ DATABASE_TABLE+"("+
                      KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+KEY_NAME+" TEXT NOT NULL,"+KEY_BEHAVE+" TEXT NOT NULL);");
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE);
                onCreate(db);

            }
        }
        public behave( Context c) {
            ourContext =c;
        }
        public behave open() throws SQLException{

            ourHelper=new Dbhelper(ourContext);
            ourDatabase=ourHelper.getWritableDatabase();
            return this;
        }
        public void close(){
            ourHelper.close();
        }
        public long createEntry(String sqlname, String sqlbehave) {

            ContentValues cv=new ContentValues();
            cv.put(KEY_NAME,sqlname);
            cv.put(KEY_BEHAVE, sqlbehave);
            return ourDatabase.insert(DATABASE_TABLE,null,cv);

        }
        public String getData() {
            String[] column = new String[]{KEY_ROWID, KEY_NAME, KEY_BEHAVE};
            Cursor c = ourDatabase.query(DATABASE_TABLE, column, null, null, null, null, null);
            String data = "";
            int iRow = c.getColumnIndex(KEY_ROWID);
            int iName = c.getColumnIndex(KEY_NAME);
            int iBehave = c.getColumnIndex(KEY_BEHAVE);
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                data = data + c.getString(iRow) + " " + c.getString(iName)+" "+c.getString(iBehave)+ "/n";
            }
            return data;
        }
    }

you should retrieve ur data using cursor like this .

Cursor c = null;
    try {


     c = db.rawQuery(sqlString, object); //here ur query comes..

    while (c.moveToNext())
    {
        CustomField customField = new CustomField();

        //get ur data here ...
    }
    } catch (Exception e) {
        // TODO: handle exception
    }


    finally {
        // this gets called even if there is an exception somewhere above
        if(c != null)
            c.close();
    }

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