由于数据库出现问题,我的应用程序崩溃了。我不确定是什么导致了这个问题。以下是更多细节。
我在 Logcat:
中收到以下错误07-24 19:05:29.471: E/AndroidRuntime(23786): FATAL EXCEPTION: Thread-24967
07-24 19:05:29.471: E/AndroidRuntime(23786): Process: com.example.brianapp, PID: 23786
07-24 19:05:29.471: E/AndroidRuntime(23786): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-24 19:05:29.471: E/AndroidRuntime(23786): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
07-24 19:05:29.471: E/AndroidRuntime(23786): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-24 19:05:29.471: E/AndroidRuntime(23786): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
07-24 19:05:29.471: E/AndroidRuntime(23786): at com.example.brianapp.DatabaseHelper.getScore(DatabaseHelper.java:116)
07-24 19:05:29.471: E/AndroidRuntime(23786): at com.example.brianapp.Meditation.writeToDatabase(Meditation.java:626)
07-24 19:05:29.471: E/AndroidRuntime(23786): at com.example.brianapp.Meditation$2.run(Meditation.java:139)
首先在我的数据库助手类中引用 this method :
// Getting single score (i.e. not the full db)
Score getScore(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SCORE, new String[] {COL_SESSION, COL_GAMETITLE,
COL_NAME, COL_MED, COL_MAX, COL_AVGATT, COL_MAXATT, COL_SCORE, COL_DATE }, COL_SESSION + "=?",
new String[] { String.valueOf(name) }, null, null,null,null);
if (cursor != null)
cursor.moveToFirst();
Score score = new Score(Integer.parseInt(cursor
.getString(0)),cursor.getString(1),cursor.getString(2),Integer.parseInt(cursor
.getString(3)),Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)),
Integer.parseInt(cursor.getString(6)),
Integer.parseInt(cursor.getString(7)), cursor.getString(8));
// return contact
return score;
}
以及此代码部分,我在其他活动中使用此方法:
public void writeToDatabase(){
DatabaseHelper db = new DatabaseHelper(this);
/**
* CRUD Operations
* */
SimpleDateFormat df= new SimpleDateFormat("dd/MM/yyyy"); //change back to original if not working (see SO)
//myDate will be used to enter into DB
String myDate= df.format(new Date());
//getting random number for sessionid (WILL BE CHANGED EVENTUALLY)
Random ran = new Random();
int x = ran.nextInt(1) + 3000;
//Adding the new Score to the database. NOTE: SESSIONID NEEDS TO BE SORTED
db.addScore(new Score(x, "Multiplication", UserName.getUserName(), averageMedLevel,medMax,
averageAttLevel,attMax, score,myDate));
//single score, used for passing to the results screen
single = db.getScore(UserName.getUserName());
//list of all the scores in the database
List<Score> scores = db.getAllScores();
答案 0 :(得分:0)
好像你的光标是空的,我建议你只在非空时从光标读取数据。在您的代码中,您已经拥有了所需的一切。只需进行下一次更改:
Score getScore(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SCORE, new String[] {COL_SESSION, COL_GAMETITLE,
COL_NAME, COL_MED, COL_MAX, COL_AVGATT, COL_MAXATT, COL_SCORE, COL_DATE }, COL_SESSION + "=?",
new String[] { String.valueOf(name) }, null, null,null,null);
//Just add brackets to this validation so you only read the Cursor when is not empty
if (cursor != null) {
cursor.moveToFirst();
Score score = new Score(Integer.parseInt(cursor
.getString(0)),cursor.getString(1),cursor.getString(2),Integer.parseInt(cursor
.getString(3)),Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)),
Integer.parseInt(cursor.getString(6)),
Integer.parseInt(cursor.getString(7)), cursor.getString(8));
return score;
}
//Otherwise return null or change this to whatever you want
else
return null
}
答案 1 :(得分:0)
当查询未返回任何数据时,没有第一行要移动,moveToFirst()
返回false
。
你必须检查一下:
Cursor cursor = db.query(...);
if (!cursor.moveToFirst()) {
// nothing found in DB
return null; // or whatever is appropiate for this error
}
... cursor.getXxx() ...
return score;
(注意:query()
永远不会返回null
。)