ApplicationListener の実装にオブジェクトを渡したいのですが、NullPointerExceptions が発生し続けるので、おそらく何か間違ったことをしているのでしょう。
次の行で例外が発生します。callback.onReady();
例外は次のとおりです (インポート ステートメントを削除したため、行番号が間違っています)。
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.dewi.jones.game_Implementation.create(game_Implementation.java:56)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:144)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
基本的には、onCreate メソッドの最後にコールバックをトリガーする必要があります。
助けてください
ありがとうございます ApplicationListener 実装の私のコードは次のとおりです。
public class game_Implementation implements ApplicationListener {
ICallbackTest callback;
public game_Implementation(ICallbackTest callback) {
this.callback = callback;
}
@Override
public void create() {
// initialize stuff
// call the onReady method at the end of the create method, when
// everything has been initialized and created
callback.onReady();
}
public void sayHello() {
System.out.println("Hello, this game is ready");
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void render() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
これが私のデスクトップゲームクラスです:
public class desktopGame {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "game";
cfg.useGL20 = true;
cfg.width = 480;
cfg.height = 320;
ICallbackTest callback = null;
final game_Implementation gameInstance = new game_Implementation(
callback);
callback = new ICallbackTest() {
@Override
public void onReady() {
gameInstance.sayHello();
}
};
LwjglApplication game = new LwjglApplication(gameInstance, cfg);
}
}
@Mel Nicholso からのアドバイスから、これ
が私の試みた解決策です。
ICallbackTest callback = null;
final game_Implementation gameInstance = null;
callback = new ICallbackTest() {
@Override
public void onReady() {
gameInstance.sayHello();
}
};
gameInstance = new game_Implementation(
callback);
LwjglApplication game = new LwjglApplication(gameInstance, cfg);
今、私はエラーメッセージを受け取っています:The final local variable gameInstance cannot be assigned. It must be blank and not using a compound assignment
助けてください、ありがとう