-1

I am trying to write a code in my game to save the score when the player dies. For that purpose I implemented an interface to use SQLiteOpenHelper (the interface is in the core project and the SQLiteOpenHelper is in the android project). For anyone doesn't know, the interface thing is for this purpose see code

The problem is that when I want to save the score (int) the game crashes and an NullPointerException appears.

My code:

In the android project, the database using SQLiteOpenHelper:

public class DataBase extends SQLiteOpenHelper implements DataBaseInterface {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "gamescore";

private static final String TABLE_SCORE = "score";

private static final String KEY_ID = "_id";
private static final String KEY_SCORE = "score_value";

public DataBase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_SCORE_TABLE = "CREATE TABLE " + TABLE_SCORE + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_SCORE + " INTEGER" + ")";

    db.execSQL(CREATE_SCORE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);

    onCreate(db);
}

public void addScore(int score) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_SCORE, score);

    db.insert(TABLE_SCORE, null, values);

    db.close();

}

public String[] getAllScores() {

    String selectQuery = "SELECT  * FROM " + TABLE_SCORE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    int i = 0;

    String[] data = new String[cursor.getCount()];

    while (cursor.moveToNext()) {

        data[i] = cursor.getString(1);

        i = i++;

    }
    cursor.close();
    db.close();

    return data;
}}

Next, the main class of the android project with the database implemented:

public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new CatchEmAll(new DataBase(this.getBaseContext())), config);
}
}

In the core project, the interface that is implemented in the database of the android project:

public interface DataBaseInterface {
void addScore(int score);
String[] getAllScores();
}

In the main class of the core project (CatchEmAll.java), the implementation of the interface:

public class CatchEmAll extends Game {

private final DataBaseInterface db;

public CatchEmAll(DataBaseInterface db) {
    this.db=db;
}

Finally the game screen of the game (in core project), where I want to save the score using that interface:

public class GameScreen implements Screen {
private Game game;
private Stage stage;
private OrthographicCamera camera;
private Viewport viewport;

private DataBaseInterface db;


...

@Override
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act();
    stage.draw();

    ...

    if (Enemy.collides(player)) {
      ...

      db.addScore(score);
    }

The null exception is in db.addScore(score);

I guess i'ts because I need to use the DatabaseInterface created in the main class of the core project (CatchemAll.java) but I don't know how can I do that and if this is the problem or not. Use Android API in libgdx is a nightmare.

logcat error:

08-23 14:13:41.961 7568-7596/borruey.catchemall E/AndroidRuntime: FATAL EXCEPTION: GLThread 282 Process: borruey.catchemall, PID: 7568 java.lang.NullPointerException: Attempt to invoke interface method 'void borruey.catchemall.databasescore.DataBaseInterface.addScore(int)' on a null object reference at borruey.catchemall.screens.GameScreen.render(GameScreen.java:182) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:495) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1571) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270) 08-23 14:13:45.978 7568-7568/borruey.catchemall E/AndroidGraphics: waiting for pause synchronization took too long; assuming deadlock and killing

4

1 回答 1

1

我不完全明白你在哪里有空异常,在哪一行?但我提到你声明KEY_SCORETEXT

String CREATE_SCORE_TABLE = "CREATE TABLE " + TABLE_SCORE + "("+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+ KEY_SCORE + " TEXT" + ")";

并尝试int为它添加:

values.put(KEY_SCORE, score);

如果这不是问题,那将是未来的问题。

于 2018-08-23T18:18:46.983 回答