读取游标行失败 window android

failed to read row of cursor window android

我在我的 android 应用程序中使用 sqlite 来处理我的会话。我希望用户注册,然后在登录后转到配置文件并在配置文件中显示一些数据库项目。 android 工作室不断显示:Unable to start activity ComponentInfo{com.example.neshat.bluedot/com.example.neshat.bluedot.profile}: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

我检查了类似的问题,其中 none 个是我的答案。我给你复制了相关代码。 这是我的 sqlitehandlre:

@Override
public void onCreate(SQLiteDatabase db)
{
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE," + KEY_TOTALPOINTS + "TEXT," + KEY_DIGIPOINTS + "TEXT,"  + KEY_UID + " TEXT,"
            + KEY_CREATED_AT + " TEXT" + ");";
    db.execSQL(CREATE_LOGIN_TABLE);

    Log.d(TAG, "Database tables created");
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);

    // Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 * */                  //      db.addUser(name, email,totalpoints, digipoints, uid, created_at);

public void addUser(String name, String email ,String totalpoints, String digipoints , String uid, String created_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    values.put(KEY_EMAIL, email); // Email
    values.put(KEY_TOTALPOINTS, totalpoints);
    values.put(KEY_DIGIPOINTS, digipoints);
    values.put(KEY_UID, uid);
    values.put(KEY_CREATED_AT, created_at); // Created At

    // Inserting Row
    long id = db.insert(TABLE_USER, null, values);
    db.close(); // Closing database connection

    Log.d(TAG, "New user inserted into sqlite: " + id);
}

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_USER;

    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("totalpoints", cursor.getString(3));
        user.put("digipoints", cursor.getString(4));
        user.put("uid", cursor.getString(5));
        user.put("created_at", cursor.getString(6));
    }
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

    return user;
}

这是我的收银机activity

private void registerUser(final String name, final String email, final String password)
{
    // Tag used to cancel the request
    String tag_string_req = "req_register";

    pDialog.setMessage("Registering ...");
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST, AppConfig.URL_REGISTER, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());
               Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();

            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error)
                {
                    // User successfully stored in MySQL
                    // Now store the user in sqlite
                    String uid = jObj.getString("uid");

                    JSONObject user = jObj.getJSONObject("user");
                    String name = user.getString("name");
                    String email = user.getString("email");
                    String totalpoints = user.getString("totalpoints");
                    String digipoints = user.getString("digipoints");
                    String created_at = user.getString("created_at");

                    Log.d("sqlite", uid);
                    Log.d("sqlite", name);
                    Log.d("sqlite", email);
                    Log.d("sqlite", totalpoints);
                    Log.d("sqlite", digipoints);
                    Log.d("sqlite", created_at);

                    // Inserting row in users table
                    db.addUser(name, email,totalpoints, digipoints, uid, created_at);

                    Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

                    // Launch login activity
                    Intent intent = new Intent(
                            RegisterActivity.this,
                            LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else
                {

                    // Error occurred in registration. Get the error
                    // message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                    Log.d("Debug", errorMsg);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.toString());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }

这是我的个人资料 activity :

 db = new SQLiteHandler(getApplicationContext());

    // session manager
    session = new SessionManager(getApplicationContext());
    txtName = (TextView) findViewById(R.id.name);
    pointsview = (TextView) findViewById(R.id.points);

    //txtEmail = (TextView) findViewById(R.id.email);
    btnLogout = (Button) findViewById(R.id.lgot);
    if (!session.isLoggedIn())
    {
        logoutUser();
    }

    //Fetching user details from sqlite
    HashMap<String, String> user = db.getUserDetails();

    String name = user.get("name");
    String email = user.get("email");
    String tpoints = user.get("totalpoints");
    String dpoints = user.get("digipoints");

    // Displaying the user details on the screen
    txtName.setText(name);
    pointsview.setText(tpoints);

这是我的 logcat:

  mple.neshat.bluedot E/CursorWindow﹕ Failed to read row 0, column 5 from a CursorWindow which has 2 rows, 5 columns.
01-24 05:59:22.362  11101-11101/com.example.neshat.bluedot D/AndroidRuntime﹕ Shutting down VM
01-24 05:59:22.362  11101-11101/com.example.neshat.bluedot E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.neshat.bluedot, PID: 11101
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.neshat.bluedot/com.example.neshat.bluedot.profile}: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 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:51)
            at com.example.neshat.bluedot.helper.SQLiteHandler.getUserDetails(SQLiteHandler.java:101)
            at com.example.neshat.bluedot.profile.onCreate(profile.java:130)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

编辑: 我替换了 user.put("name", cursor.getString(1));与 user.put("name", cursor.getString(cursor.getColumnIndex("name")))

但我的错误已更改为:

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:51)
        at com.example.neshat.bluedot.helper.SQLiteHandler.getUserDetails(SQLiteHandler.java:100)
        at com.example.neshat.bluedot.profile.onCreate(profile.java:130)

总分列有错误,我不明白为什么。

尝试从 Cursor 使用此方法而不是直接传递索引:

/**
 * Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
 * If you expect the column to exist use {@link #getColumnIndexOrThrow(String)} instead, which
 * will make the error more clear.
 *
 * @param columnName the name of the target column.
 * @return the zero-based column index for the given column name, or -1 if
 * the column name does not exist.
 * @see #getColumnIndexOrThrow(String)
 */
int getColumnIndex(String columnName);

然后你会得到这样的东西:

user.put("email", cursor.getString(cursor.getColumnIndex("email")));

它可能会解决你的问题。

祝你好运。

SELECT * 结果集中没有第 5 列。

看起来您已经向 CREATE TABLE 添加了列,但您忘记了增加数据库版本来升级它,或者忘记卸载您的应用程序以摆脱旧数据库。有关详细信息,请参阅 When is SQLiteOpenHelper onCreate() / onUpgrade() run?

请注意,您的 CREATE TABLE 中也存在一些白色 space 问题:列名和类型之间缺少 space。再次修复 运行 您的代码之前的那些,以免在例如 "no such column" 上出现 "no such column" 错误插入。