无法从CursorWindow读取第0行,第2列

时间:2015-03-23 06:44:55

标签: java android sql

我尝试使用上下文菜单从listview中删除数据,我得到了这个java.lang.IllegalStateException:无法从CursorWindow读取第0行,第2列。在从中访问数据之前,请确保Cursor已正确初始化。

       public List<AssignmentRecord> getAllAssignment(){
    List<AssignmentRecord> records = new ArrayList<AssignmentRecord>();
    Cursor cursor = database.query(AssigmentContract.Assigment.TABLE_NAME, allColumn , null,
            null, null, null, null);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        AssignmentRecord assignmentRecord = new AssignmentRecord();
        assignmentRecord.setAssname(cursor.getString(0));
        assignmentRecord.setAssTime(cursor.getString(1));
        assignmentRecord.setAssID(cursor.getString(2));
        records.add(assignmentRecord);
        cursor.moveToNext();
    }

第一行错误assignmentRecord.setAssID(cursor.getString(2));

     dbcon.open();
    List<AssignmentRecord> records = dbcon.getAllAssignment();
    dbcon.close();

第二个错误是List records = dbcon.getAllAssignment();

private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
        + AssigmentContract.Assigment.TABLE_NAME +
                "(" + AssigmentContract.Assigment.COLUMN_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                AssigmentContract.Assigment.COLUMN_ASS + " TEXT," +
                AssigmentContract.Assigment.COLUMN_ASSDATE+ " TEXT)";


       public final class AssigmentContract {
              public AssigmentContract(){}

public static abstract class Assigment implements BaseColumns{
    public static final String TABLE_NAME ="AssignmentV2";
    public static final String COLUMN_ASS ="AssignmentTitle";
    public static final String COLUMN_ASSDATE ="AssignmentDate";
    public static final String COLUMN_ID ="AssignmentID";
      }
       }

的logcat

     my.com.chyi.schedulev2 E/CursorWindow﹕ Failed to read row 0, column 2 from a CursorWindow which has 1 rows, 2 columns.
     my.com.chyi.schedulev2 D/AndroidRuntime﹕ Shutting down VM
           my.com.chyi.schedulev2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x9e4f5908)
         /my.com.chyi.schedulev2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.com.chyi.schedulev2/my.com.chyi.schedulev2.assigmentActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        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:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 2 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:434)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        at my.com.chyi.schedulev2.SQLController.getAllAssignment(SQLController.java:102)
        at my.com.chyi.schedulev2.assigmentActivity.onCreate(assigmentActivity.java:73)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)                 at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230 在android.app.ActivityThread.access $ 600(ActivityThread.java:141)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234在android.os.Handler.dispatchMessage(Handler.java:99)上。 os.Looper.loop(Looper.java:137)位于java.lang.reflect.Method的java.lang.reflect.Method.invokeNative(Native Method)的android.app.ActivityThread.main(ActivityThread.java:5041)。调用(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 在dalvik.system.NativeStart.main(本地方法)

2 个答案:

答案 0 :(得分:0)

您的getAllAssignment()应为,

List<AssignmentRecord> records = new ArrayList<AssignmentRecord>();
SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery("select * from " + TABLE_NAME, null);

    if(cursor.moveToFirst()){
        do {
            AssignmentRecord assignmentRecord = new AssignmentRecord();
            assignmentRecord.setAssID(cursor.getString(0));
            assignmentRecord.setAssname(cursor.getString(1));
            assignmentRecord.setAssTime(cursor.getString(2));
        } while(cursor.moveToNext());
    }
    database.close();

答案 1 :(得分:0)

试试这个,

while(!cursor.isAfterLast()){
        AssignmentRecord assignmentRecord = new AssignmentRecord();
        assignmentRecord.setAssname(cursor.getString(cursor.getColumnIndex("your_respective_column_name));
        assignmentRecord.setAssTime(cursor.getString(cursor.getColumnIndex("your_respective_column_name)));
        assignmentRecord.setAssID(cursor.getString(cursor.getColumnIndex("your_respective_column_name)));
        records.add(assignmentRecord);
        cursor.moveToNext();

我已经证明了Harry建议你做的事情。这里将your_respective列名替换为您尝试检索的列的值。对于Ex:当你试图获取AssId的值时,它将是_id或某些东西。希望这会对你有所帮助。