我一直在与 AndEngine 合作并开发游戏。我现在已经开始尝试实现锁定级别等的分数。我一直试图让这个 sqlite 数据库运行几个小时,但实际上无济于事。如果我尝试从 1 级到 3 级,它会引发错误,但仍然可以正常工作,但是当我实施第四级或第五级时,我无法访问这些条目并强制关闭。我得到的确切错误是光标超出范围异常,并且从未明确调用过 close()。任何帮助表示赞赏。
这是我的数据库类
package com.example.test;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class myDatabase extends SQLiteOpenHelper {
static final String dbName = "myDB";
static final String tLevels = "Levels";
static final String fLevelID = "levelNum";
static final String fLevelUnLocked = "levelLocked";
static final String fLevelBeat = "levelBeat";
static final String fLevelScore = "levelScore";
public myDatabase(Context context) {
super(context, dbName, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "+tLevels+" (" +
fLevelID + " INTEGER PRIMARY KEY , " +
fLevelUnLocked + " TEXT, " +
fLevelBeat + " TEXT, " +
fLevelScore + " TEXT" +
")");
ContentValues cv = new ContentValues();
cv.put(fLevelID, 1);
cv.put(fLevelUnLocked, "true");
cv.put(fLevelBeat, "false");
cv.put(fLevelScore, "0");
db.insert(tLevels, null, cv);
cv.put(fLevelID, 2);
cv.put(fLevelUnLocked, "false");
cv.put(fLevelBeat, "false");
cv.put(fLevelScore, "0");
db.insert(tLevels, null, cv);
cv.put(fLevelID, 3);
cv.put(fLevelUnLocked, "false");
cv.put(fLevelBeat, "false");
cv.put(fLevelScore, "0");
db.insert(tLevels, null, cv);
cv.put(fLevelID, 4);
cv.put(fLevelUnLocked, "false");
cv.put(fLevelBeat, "false");
cv.put(fLevelScore, "0");
db.insert(tLevels, null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+tLevels);
onCreate(db);
}
public String isLevelUnLocked(int ID) {
SQLiteDatabase myDB = this.getReadableDatabase();
String[] mySearch = new String[]{String.valueOf(ID)};
Cursor myCursor = myDB.rawQuery("SELECT "+ fLevelUnLocked +" FROM "+ tLevels +" WHERE "+ fLevelID +"=?",mySearch);
myCursor.moveToFirst();
int index = myCursor.getColumnIndex(fLevelUnLocked);
String myAnswer = myCursor.getString(index);
myCursor.close();
myDB.close();
return myAnswer;
}
public String isBeat(int ID) {
SQLiteDatabase myDB = this.getReadableDatabase();
String[] mySearch = new String[]{String.valueOf(ID)};
Cursor myCursor = myDB.rawQuery("SELECT "+ fLevelBeat +" FROM "+ tLevels +" WHERE "+ fLevelID +"=?",mySearch);
myCursor.moveToFirst();
int index = myCursor.getColumnIndex(fLevelBeat);
String myAnswer = myCursor.getString(index);
myCursor.close();
return myAnswer;
}
public int unLockLevel(int ID, String isUnLocked)
{
SQLiteDatabase myDB = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(fLevelUnLocked, isUnLocked);
int numRowsAffected = myDB.update(tLevels, cv, fLevelID+"=?", new String []{String.valueOf(ID)});
return numRowsAffected;
}
}
这是我称之为的主要活动
myDatabase myDB = new myDatabase(this);
final String checkUnLocked = myDB.isLevelUnLocked(4);
System.out.println(checkUnLocked);
if(checkUnLocked.compareTo("true") == 0){
System.out.println("level one is unlocked!");
}else{
System.out.println("level one is not unlocked!");
}
myDB.close();
这是日志猫
12-27 13:40:14.896: E/AndEngine(4819): [Debug.java:372:e()] World1Activity.onCreateScene failed. @(Thread: 'GLThread 13')
12-27 13:40:14.896: E/AndEngine(4819): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-27 13:40:14.896: E/AndEngine(4819): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
12-27 13:40:14.896: E/AndEngine(4819): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
12-27 13:40:14.896: E/AndEngine(4819): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
12-27 13:40:14.896: E/AndEngine(4819): at com.example.test.myDatabase.isLevelUnLocked(myDatabase.java:97)
12-27 13:40:14.896: E/AndEngine(4819): at com.example.test.World1Activity.onCreateScene(World1Activity.java:159)
12-27 13:40:14.896: E/AndEngine(4819): at org.andengine.ui.activity.SimpleBaseGameActivity.onCreateScene(SimpleBaseGameActivity.java:48)
12-27 13:40:14.896: E/AndEngine(4819): at org.andengine.ui.activity.BaseGameActivity$3.onCreateResourcesFinished(BaseGameActivity.java:169)
12-27 13:40:14.896: E/AndEngine(4819): at org.andengine.ui.activity.SimpleBaseGameActivity.onCreateResources(SimpleBaseGameActivity.java:43)
12-27 13:40:14.896: E/AndEngine(4819): at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:181)
12-27 13:40:14.896: E/AndEngine(4819): at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:110)
12-27 13:40:14.896: E/AndEngine(4819): at org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80)
12-27 13:40:14.896: E/AndEngine(4819): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1348)
12-27 13:40:14.896: E/AndEngine(4819): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
12-27 13:40:14.896: D/AndEngine(4819): [Debug.java:218:d()] World1Activity.onSurfaceChanged(Width=800, Height=480) @(Thread: 'GLThread 13')
12-27 13:40:15.076: E/Database(4819): [SQLiteDatabase.java:1825:finalize()] close() was never explicitly called on database '/data/data/com.example.test/databases/myDB'
12-27 13:40:15.076: E/Database(4819): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
12-27 13:40:15.076: E/Database(4819): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
12-27 13:40:15.076: E/Database(4819): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
12-27 13:40:15.076: E/Database(4819): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
12-27 13:40:15.076: E/Database(4819): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
12-27 13:40:15.076: E/Database(4819): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:579)
12-27 13:40:15.076: E/Database(4819): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-27 13:40:15.076: E/Database(4819): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
12-27 13:40:15.076: E/Database(4819): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
12-27 13:40:15.076: E/Database(4819): at com.example.test.myDatabase.isLevelUnLocked(myDatabase.java:92)
12-27 13:40:15.076: E/Database(4819): at com.example.test.World1Activity.onCreateScene(World1Activity.java:159)
12-27 13:40:15.076: E/Database(4819): at org.andengine.ui.activity.SimpleBaseGameActivity.onCreateScene(SimpleBaseGameActivity.java:48)
12-27 13:40:15.076: E/Database(4819): at org.andengine.ui.activity.BaseGameActivity$3.onCreateResourcesFinished(BaseGameActivity.java:169)
12-27 13:40:15.076: E/Database(4819): at org.andengine.ui.activity.SimpleBaseGameActivity.onCreateResources(SimpleBaseGameActivity.java:43)
12-27 13:40:15.076: E/Database(4819): at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:181)
12-27 13:40:15.076: E/Database(4819): at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:110)
12-27 13:40:15.076: E/Database(4819): at org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80)
12-27 13:40:15.076: E/Database(4819): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1348)
12-27 13:40:15.076: E/Database(4819): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)