当前位置: 首页 > 知识库问答 >
问题:

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

胡墨竹
2023-03-14

我尝试使用上下文菜单从listview中删除数据,得到了java.lang.IllegalStateException:Could not read row 0,col 2 from CursorWindow。在从游标访问数据之前,请确保游标已正确初始化。

       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";
      }
       }
     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.Access$H.HandleMessage(ActivityThread.java:1234,Android.os.Handler.DispatchMessage(ActivityThread.java:99),Android.os.Looper.Loop(ActivityThread.java:137),Android.app.ActivityThread.Main(

共有1个答案

终育
2023-03-14

试试这个,

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

我已经演示了哈里建议你做的事。在这里,用您为试图检索的列给定的值替换您各自的列名。对于ex:当您试图获取AssId的值时,它将是_id或其他东西。希望这对你有帮助。

 类似资料: