Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法读取光标窗口的行_Java_Android_Sqlite_Android Logcat - Fatal编程技术网

Java 无法读取光标窗口的行

Java 无法读取光标窗口的行,java,android,sqlite,android-logcat,Java,Android,Sqlite,Android Logcat,我在android应用程序中使用sqlite来处理会话。我希望用户注册,然后在登录后转到配置文件,并显示配置文件中的一些db项。 android studio不断显示:无法启动活动组件信息{com.example.neshat.bluedot/com.example.neshat.bluedot.profile}:java.lang.IllegalStateException:无法从游标窗口读取第0行第5列。在从光标访问数据之前,请确保光标已正确初始化。 我检查了类似的问题,没有一个是我的答案

我在android应用程序中使用sqlite来处理会话。我希望用户注册,然后在登录后转到配置文件,并显示配置文件中的一些db项。 android studio不断显示:
无法启动活动组件信息{com.example.neshat.bluedot/com.example.neshat.bluedot.profile}:java.lang.IllegalStateException:无法从游标窗口读取第0行第5列。在从光标访问数据之前,请确保光标已正确初始化。

我检查了类似的问题,没有一个是我的答案。我为您复制了相关代码。 这是我的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;
}
编辑: 我替换了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)

totalpoints列出现错误,我无法理解原因。

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

/**
 * 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列

看起来您已经在
创建表中添加了列,但是您忘记了增加数据库版本以进行升级,或者卸载应用程序以删除旧数据库。更多信息请参见


请注意,
创建表
中还存在两个空格问题:列名和类型之间缺少空格。在再次运行代码之前修复这些错误,以避免在插入时出现“无此类列”错误。

u的意思是例如,而不是user.put(“email”,cursor.getString(1));我应该使用cursor.getColumnIndex(“email”);?您应该使用user.put(“email”)、cursor.getString(cursor.getColumnIndex(“email”));
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)
/**
 * 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")));