2017-03-17 64 views
-2

访问数据,我得到一个错误,无法读取行0,列-1从CursorWindow之前正确初始化。在从中访问数据之前,确保Cursor已正确初始化。 我已经尝试了一切,但似乎没有任何工作错误:无法读取行0,列-1从CursorWindow。确保光标从它

变量:

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) 

谢谢您的帮助

+1

使用'getColumnIndexOrThrow'代替'getColumnIndex' – pskink

+1

请,除去'+ “其中1;”;'。这不仅是** **无用,而且可能有害** **(它会使你的数据库容易'SQL Injection') –

+0

错误本身只是意味着**“没有这样的列” ** ...这是为什么不使用'select *'更好,因为你不知道你会得到什么列 – Selvin

回答

3

在列名中删除前导空格:

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不关心这样的空白,但像地图,内部表示游标希望看到的列完全一样的SQL(其中的空格被忽略)。

+0

这很有用,谢谢。非常感谢! –

相关问题