我几乎所有的东西都很好,并且检查了与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)
答案 0 :(得分:2)
我从那里拿到示例(http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/)的那个人从cursor.getString(1)开始计数的地方;从ONE然而它应该以零'0'开始,现在解决了!