我收到错误无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保正确初始化Cursor。 我已经尝试了一切,但似乎没有任何工作
变量:
private static final String COLUMN_SERVICE_DETAILS = " service_details";
private static final String COLUMN_SERVICE_TIME = " service_time";
private static final String COLUMN_SERVICE_COST = " service_cost";
private static final String TABLE_DETAILS = "details";
表格创建
query = "CREATE TABLE " +TABLE_DETAILS+"("
+COLUMN_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+COLUMN_SERVICE_TIME+ " TEXT, "
+COLUMN_SERVICE_DETAILS+ " TEXT, "
+COLUMN_SERVICE_COST+ " TEXT " +
");";
sqLiteDatabase.execSQL(query);
调试我的应用程序后,它停止在此方法上 的 getUserDetails
public UserDetails getUserDetails(){
UserDetails ud = new UserDetails();
String selectQuery = "select * from " + TABLE_DETAILS + " where 1;";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
ud.set_service_details(cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_DETAILS)));
ud.set_service_time(cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_TIME)));
ud.set_service_cost(cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_COST)));
cursor.moveToNext();
}
cursor.close();
db.close();
return ud;
}
错误日志:
03-17 17:27:03.553 14627-14627/com.automobilecare.automobilecare E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.automobilecare.automobilecare, PID: 14627
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.automobilecare.automobilecare/com.automobilecare.automobilecare.userArea.UserAreaActivity}: 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:2584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.Activitat android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
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:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:66)
at com.automobilecare.automobilecare.internalDBConnectivity.DBHandler.getUserDetails(DBHandler.java:240)
at com.automobilecare.automobilecare.userArea.UserAreaActivity.onCreate(UserAreaActivity.java:85)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
感谢您的帮助
答案 0 :(得分:4)
删除列名称中的前导空格:
private static final String COLUMN_SERVICE_DETAILS = " service_details";
private static final String COLUMN_SERVICE_TIME = " service_time";
private static final String COLUMN_SERVICE_COST = " service_cost";
更改为
private static final String COLUMN_SERVICE_DETAILS = "service_details";
private static final String COLUMN_SERVICE_TIME = "service_time";
private static final String COLUMN_SERVICE_COST = "service_cost";
SQL并不关心这样的空白,而是像Map一样的内部表示,Cursor希望看到与SQL完全相同的列(忽略空格)。