Hello. I’ve created an appstate - MenuState - and I believe it’s been initialised correctly. However, when I call a method from an associated Nifty screen, it throws a null exception when accessing the App.
Here’s how the app state is created in Main:
[java]
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
guiViewPort.addProcessor(niftyDisplay);
nifty = niftyDisplay.getNifty();
menuState = new MenuState();
stateManager.attach(menuState);
nifty.addXml(“Interface/Screens/start.xml”);
menuState.setEnabled(false);[/java]
Here’s how it’s initialised (I’ve just included initialize() and bind()):
[java]
public SimpleApplication app;
private Nifty nifty;
private Screen screen;
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication)app;
}
@Override
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
[/java]
And when I call this MenuState function via a Nifty onClick interaction:
[java]
public void niftyMethod() {System.out.println("App ref: " + this.app); }
[/java]
I get “App ref: null”.
Have I created the app state correctly, and if so, how do I get access to the App?
EDIT:
I fixed it by altering my simpleInitApp() method thus:
[java]
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
nifty = niftyDisplay.getNifty();
nifty.addXml("Interface/Screens/start.xml");
nifty.addXml("Interface/Screens/game.xml");
menuState = (MenuState) nifty.getScreen("start").getScreenController();
stateManager.attach(menuState);
gameState = (GameState) nifty.getScreen("game").getScreenController();
stateManager.attach(gameState);
nifty.gotoScreen("start");
menuState.setEnabled(true);
guiViewPort.addProcessor(niftyDisplay);
[/java]
I assume this is the recommended way to register screen controllers when working with AbstractAppStates and individual Nifty XML files?