我收到一个android.database.CursorIndexOutOfBoundsException:索引0请求,当尝试列出Sqlite数据库中的数据时,大小为0错误。通过阅读具有相同错误的其他问题,我可以看出第一条记录存在空值。分辨率似乎是关闭光标,但我已经这样做了,所以这不是原因。除非我错误地关闭光标。我发布了我收到的错误以及我认为与错误相关的代码。有关如何解决此错误的任何指导将不胜感激。
Process: ca.rvogl.tpbcui, PID: 30793
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
at ca.rvogl.tpbcui.Database.DatabaseHelper.getGame(DatabaseHelper.java:445)
at ca.rvogl.tpbcui.Views.GameActivity.createGame(GameActivity.java:113)
at ca.rvogl.tpbcui.Views.GameActivity.access$800(GameActivity.java:30)
at ca.rvogl.tpbcui.Views.GameActivity$7.onClick(GameActivity.java:244)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我的DatabaseHelper中的代码
public Game getGame(String leagueId, String bowlerId, String seriesId) {
//Get Readable Database If We Are Not Inserting Anything
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query( Game.TABLE_NAME,
new String[]{Game.COLUMN_ID, Game.COLUMN_LEAGUE_ID, Game.COLUMN_BOWLER_ID,
Game.COLUMN_SERIES_ID, Game.COLUMN_SCORE, Game.COLUMN_STRIKES, Game.COLUMN_SPARES,Game.COLUMN_SPLITS, Game.COLUMN_SPLIT_CONVERSIONS, Game.COLUMN_OPEN_FRAMES, Game.COLUMN_TIMESTAMP},
Game.COLUMN_LEAGUE_ID + "=?" + " AND " + Game.COLUMN_BOWLER_ID + "=?" + " AND " + Game.COLUMN_SERIES_ID + "=?",
new String[]{String.valueOf(leagueId), String.valueOf(bowlerId), String.valueOf(seriesId)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
//Prepare Game Object
Game game = new Game(
cursor.getInt(cursor.getColumnIndex(Game.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_LEAGUE_ID)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_BOWLER_ID)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_SERIES_ID)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_SCORE)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_STRIKES)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_SPARES)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_SPLITS)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_SPLIT_CONVERSIONS)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_OPEN_FRAMES)),
cursor.getString(cursor.getColumnIndex(Game.COLUMN_TIMESTAMP)));
//Close Database Connection
cursor.close();
return game;
}
public List<Game> getAllGames(String leagueId, String bowlerId, String seriesId) {
List<Game> games = new ArrayList<>();
//Select All Query
String selectQuery = "SELECT * FROM " + Game.TABLE_NAME + " WHERE " + Game.COLUMN_LEAGUE_ID + " = '" + leagueId + "'" + " AND " + Game.COLUMN_BOWLER_ID + " = '" + bowlerId + "'" + " AND " + Game.COLUMN_SERIES_ID + " = '" + seriesId + "'" + " ORDER BY " +
Game.COLUMN_TIMESTAMP + " DESC";
try (SQLiteDatabase db = this.getWritableDatabase()) {
@SuppressLint("Recycle") Cursor cursor = db.rawQuery( selectQuery, null );
//Looping Through All Rows And Adding To The List
if (cursor.moveToFirst()) {
do {
Game game1 = new Game();
game1.setId(cursor.getInt(cursor.getColumnIndex(Game.COLUMN_ID ) ) );
game1.setLeagueId(cursor.getString(cursor.getColumnIndex(Game.COLUMN_LEAGUE_ID)));
game1.setBowlerId(cursor.getString(cursor.getColumnIndex(Game.COLUMN_BOWLER_ID)));
game1.setSeriesId(cursor.getString(cursor.getColumnIndex(Game.COLUMN_SERIES_ID)));
game1.setScore(cursor.getString(cursor.getColumnIndex(Game.COLUMN_SCORE)));
game1.setSpares(cursor.getString(cursor.getColumnIndex(Game.COLUMN_SPARES)));
game1.setSplits(cursor.getString(cursor.getColumnIndex(Game.COLUMN_SPLITS)));
game1.setSplitConversions(cursor.getString(cursor.getColumnIndex(Game.COLUMN_SPLIT_CONVERSIONS)));
game1.setOpenFrames(cursor.getString(cursor.getColumnIndex(Game.COLUMN_OPEN_FRAMES)));
game1.setTimestamp(cursor.getString(cursor.getColumnIndex(Game.COLUMN_TIMESTAMP)));
games.add( game1 );
} while (cursor.moveToNext());
}
//Close Database Connection
db.close();
}
//Return Game List
return games;
}
来自Games.java的代码
//Inserting New Game In The Database & Refreshing The List
private void createGame(String leagueId, String bowlerId, String seriesId, String score, String spares, String strikes, String splits, String splitConversions, String openFrames) {
//Inserting Game In The Database & Getting Newly Inserted Game Id
long id = db.insertGame(leagueId, bowlerId, seriesId, score, strikes, spares, splits, splitConversions, openFrames);
//Getting The Newly Inserted Game From The Database
Game n = db.getGame(leagueId, bowlerId, seriesId);
if (n != null) {
//Adding New Game To The Array at Position 0
gameList.add(0, n);
//Refreshing The List
mAdapter.notifyDataSetChanged();
toggleEmptyGames();
}
}
//Updating Game In The Database & Updating Item In The List By Its Position
private void updateGame(String score, String strikes, String spares, String splits, String splitConversions, String openFrames, int position) {
Game n = gameList.get(position);
//Updating the Game Text
n.setScore(score);
n.setStrikes(strikes);
n.setSpares(spares);
n.setSplits(splits);
n.setSplitConversions(splitConversions);
n.setOpenFrames(openFrames);
//Updating Game In The Database
db.updateGame(n);
//Refreshing The List
gameList.set(position, n);
mAdapter.notifyItemChanged(position);
toggleEmptyGames();
}
//Deleting Game From SQWLite & Removing The Item From The List By Its Position
private void deleteGame(int position) {
//Deleting Game From The Database
db.deleteGame(gameList.get(position));
//Removing Game From The List
gameList.remove(position);
mAdapter.notifyItemRemoved(position);
toggleEmptyGames();
}
//Opens Dialog With Edit - Delete Options
private void showActionsDialog(final int position) {
CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose option");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
showGameDialog(true, gameList.get(position), position);
} else {
deleteGame(position);
}
}
});
builder.show();
}
此错误仅在我尝试将另一个游戏添加到数据库时出现。当我重新启动应用程序时,输入的数据出现在列表视图中,但是不正确的是某些字段中缺少值。
修订代码
public Game getGame(String leagueId, String bowlerId, String seriesId) {
//Get Readable Database If We Are Not Inserting Anything
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query( Game.TABLE_NAME,
new String[]{Game.COLUMN_ID, Game.COLUMN_LEAGUE_ID, Game.COLUMN_BOWLER_ID,
Game.COLUMN_SERIES_ID, Game.COLUMN_SCORE, Game.COLUMN_STRIKES, Game.COLUMN_SPARES, Game.COLUMN_SPLITS, Game.COLUMN_SPLIT_CONVERSIONS, Game.COLUMN_OPEN_FRAMES, Game.COLUMN_TIMESTAMP},
Game.COLUMN_LEAGUE_ID + "=?" + " AND " + Game.COLUMN_BOWLER_ID + "=?" + " AND " + Game.COLUMN_SERIES_ID + "=?",
new String[]{String.valueOf( leagueId ), String.valueOf( bowlerId ), String.valueOf( seriesId )}, null, null, null, null );
// if (cursor.getCount()>0)
// cursor.moveToFirst();
// Log.d("Events",Integer.toString(cursor.getCount()));
if (cursor.moveToFirst()) {
//Prepare Game Object
Game game = new Game(
cursor.getInt( cursor.getColumnIndex( Game.COLUMN_ID ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_LEAGUE_ID ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_BOWLER_ID ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_SERIES_ID ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_SCORE ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_STRIKES ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_SPARES ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_SPLITS ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_SPLIT_CONVERSIONS ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_OPEN_FRAMES ) ),
cursor.getString( cursor.getColumnIndex( Game.COLUMN_TIMESTAMP ) ) );
//Close Database Connection
cursor.close();
return game;
} else {
}
}
答案 0 :(得分:1)
您收到此错误的原因有两方面。首先,查询返回的行没有正确检查/处理,因为当从query
方法返回的Cursor不会检查 null Cursor 时为空。它可能是为空(cursor.getCount()将返回0)。
所有Cursor move????
方法都会返回true或false,具体取决于是否可以进行移动。
为了正确处理阅读光标,你应该从不使用(这是无用的,在你的情况下是破坏性的): -
if (cursor != null) {
cursor.moveToFirst(); // (or any move???? method
..... Ouch if the Cursor is empty as there will be no first row to extract data from
}
而应该使用: -
if (cursor.moveToFirst()) {
..... do your stuff here
} else {
..... handle no row found here if wanted
}
通过Cursor进行循环,您可以使用: -
while (cursor.moveToNext()) {
.... handle each row here
}
如果需要循环游标并且不处理游标中的行,可以使用: -
if (cursor.getCount() < 1) {
..... handle no rows here
}
while (cursor.moveToNext()) {
..... handle each row here
}
注意到在任何情况下都应该在完成时关闭光标。