我几乎所有东西都很好,并且四次检查了与 db 和光标相关的所有其他东西,我在一个活动中有一个按钮,单击该按钮运行以下代码以从其他类中的公共函数访问字符串:
DatabaseHandler dbfunc = new DatabaseHandler(getApplicationContext());
String deviceCODE = dbfunc.getUSERdetailsSTRING();
Log.i(TAG, deviceCODE);
以下是 DB 处理程序类中的公共函数:
public String getUSERdetailsSTRING(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
user.put("Device Code", cursor.getString(5));
}
cursor.moveToFirst();
String deviceCODE = user.get(KEY_UID);
return deviceCODE;
}
但它在 logcat 上向我显示以下错误:
08-16 22:06:24.840: E/AndroidRuntime(25923): FATAL EXCEPTION: main
08-16 22:06:24.840: E/AndroidRuntime(25923): java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-16 22:06:24.840: E/AndroidRuntime(25923): at android.database.CursorWindow.nativeGetString(Native Method)
08-16 22:06:24.840: E/AndroidRuntime(25923): at android.database.CursorWindow.getString(CursorWindow.java:438)
08-16 22:06:24.840: E/AndroidRuntime(25923): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)