gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:02:12 25 4
gpt4 key购买 nike

我尝试使用上下文菜单从 ListView 中删除数据,但出现 java.lang.IllegalStateException: Couldn't 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();
}

第一行错误赋值Record.setAssID(cursor.getString(2));

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

第二个错误是列出记录= 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) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5041) 在 java.lang.reflect.Method.in vokeNative( native 方法)at java.lang.reflect.Method.invoke(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( native 方法)

最佳答案

试试这个,

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

我已经演示了哈利建议你做的事情。此处将 your_respective 列名称替换为您为尝试检索的列指定的值。例如:当您尝试获取 AssId 的值时,它将是 _id 或其他东西。希望这对您有帮助。

关于java - 无法从 CursorWindow 读取第 0 行第 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29204577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com