2

我几乎所有东西都很好,并且四次检查了与 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)
4

1 回答 1

2

The place from where I took the example(http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/) that guy started the counting from cursor.getString(1); from ONE However it should be started with a ZERO '0', its solved now!

于 2013-08-16T16:55:29.233 回答