我在onTreate()方法中创建了一个类似Players
的表,它位于MySQLiteHelper.java中:
String CREATE_PLAYERS_TABLE = "CREATE TABLE Players
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password TEXT, score INTEGER)";
db.execSQL(CREATE_PLAYERS_TABLE);
我在同一个类中有一个更新方法,如:
public int PlayerUpdate(Player PL) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("name", PL.getName());
values.put("password", PL.getPassword());
values.put("score", PL.getScore());
// 3. updating row
int i = db.update(TABLE_Players, // table
values, // column/value
KEY_ID + " = ?", // selections
new String[] { String.valueOf(PL.getId()) }); // selection args
// 4. close
db.close();
return i;
}
当我调用函数将score
变量更改为:
public void scoreUp() {
helper.PlayerUpdate(new Player(player_name,player_password,player_score+5));
}
public void scoreDown() {
helper.PlayerUpdate(new Player(player_name,player_password,player_score-5));
}
我收到的logcat错误如下:
Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.
Make sure the Cursor is initialized correctly before accessing data from it.
感谢。
还有另一个查询导致了这个特定的异常,我发现了。
答案 0 :(得分:5)
异常来自访问游标列,而您发布的代码没有显示。
但是,列索引从0开始,因此要将第一列值作为字符串访问,请使用getString(0)
而不是getString(1)
。