How to use Nifty GUI on Android?

Cant get nifty to work with android correctly …

I made this screen with nifty:
startScreen

If i click Load Game the loadGame screen appears as it should:
loadScreen

But if i click on Start, nothing happens. I saw I am getting an Warning:
W/de.lessvoid.nifty.elements.Element(30590): WARNING missing element/control with id [StartButton#text] for requested control class [de.lessvoid.nifty.controls.TextField]

My screen.xml is:
[java]
<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />
<registerSound id=“myclick” filename=“Sounds/GUI/ButtonClick.ogg” />

<!-- rezise Image, mark the edges"15,x,x,x" … and stretches it -->
<controlDefinition name = “loadingbar” controller = “mygame.Game”>
<image filename=“Interface/border.png” childLayout=“absolute”
imageMode=“resize:15,2,15,15,15,2,15,2,15,2,15,15”>
<image id=“progressbar” x=“0” y=“0” filename=“Interface/inner.png” width=“32px” height=“100%”
imageMode=“resize:15,2,15,15,15,2,15,2,15,2,15,15” />
</image>
</controlDefinition>

<screen id=“startScreen” controller=“mygame.StartScreenState” >
<layer id=“background” backgroundImage=“Interface/StartScreen.png” childLayout=“center”></layer>
<layer id=“foreground” childLayout=“vertical”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text font=“Interface/Fonts/Default.fnt” color="#000f" width=“100%” height=“100%” text=“Welcome” />
</panel>
<panel id=“panel_mid” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text font=“Interface/Fonts/Default.fnt” color="#000f" width=“100%” height=“100%” wrap=“true” text=“Your job is Find and Solve the forgotten hints in the world” />
</panel>
<panel id=“panel_bottom” height=“50%” width=“75%” align=“center” childLayout=“horizontal” >
<panel id=“panel_bottom_left” height=“90%” width=“60%” childLayout=“center”></panel>
<panel id=“panel_bottom_right” height=“90%” width=“40%” childLayout=“vertical”>
<panel id=“startPanel” height=“30%” width=“100%” childLayout=“center”>
<control name=“button” label=“Start” id=“StartButton” height=“100%” width=“100%” align=“center” valign=“center” visibleToMouse=“true” >
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“loadGameSave(0)”/>
</control>
</panel>
<panel id=“loadPanel” height=“40%” width=“100%” childLayout=“center”>
<control name=“button” label=“Load Game” id=“LoadGame” height=“75%” width=“100%” align=“center” valign=“center” visibleToMouse=“true”>
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“nextScreen(loadScreen)”/>
</control>
</panel>
<panel id="quitPanel " height=“30%” width=“100%” childLayout=“center”>
<control name=“button” label=“Quit” id=“QuitButton” height=“100%” width=“100%” align=“center” valign=“center” visibleToMouse=“true”>
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“exit()”/>
</control>
</panel>
</panel>
</panel>
</layer>
</screen>

<screen id=“loadScreen” controller=“mygame.StartScreenState”>
<layer id=“background” backgroundImage=“Interface/LoadScreen.png” childLayout=“center”></layer>
<layer id=“foreground” childLayout=“vertical”>
<panel id=“panel1” height=“40%” width=“75%” align=“center” childLayout=“vertical”> </panel>
<panel id=“panel2” height=“25%” width=“75%” align=“center” childLayout=“horizontal”>
<panel id=“panel_left” height=“100%” width=“30%” valign=“center” childLayout=“center”>
<control name=“button” label=“Save 1” id=“SaveGame1” height=“100%” width=“100%” align=“center” valign=“center” visibleToMouse=“true” >
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“loadGameSave(1)”/>
</control>
</panel>
<panel id=“panel_center” height=“100%” width=“40%” valign=“center” childLayout=“center”>
<control name=“button” label=“Save 2” id=“SaveGame2” height=“100%” width=“75%” align=“center” valign=“center” visibleToMouse=“true”>
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“loadGameSave(2)”/>
</control>
</panel>
<panel id=“panel_right” height=“100%” width=“30%” valign=“center” childLayout=“center”>
<control name=“button” label=“Save 3” id=“SaveGame3” height=“100%” width=“100%” align=“center” valign=“center” visibleToMouse=“true”>
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“loadGameSave(3)”/>
</control>
</panel>
</panel>
<panel id=“spacePanel3” height=“25%” width=“75%” align=“center” childLayout=“horizontal”>
<panel id=“panel31” height=“100%” width=“30%” valign=“center” childLayout=“center”></panel>
<panel id=“panel32” height=“100%” width=“40%” valign=“center” childLayout=“center”></panel>
<panel id=“panel33” height=“100%” width=“30%” valign=“center” childLayout=“center”>
<control name=“button” label=“Back” id=“backButton” height=“50%” width=“100%” align=“center” valign=“center” visibleToMouse=“true” >
<effect>
<onClick name=“playSound” sound=“myclick”/>
</effect>
<interact onClick=“nextScreen(startScreen)”/>
</control>
</panel>
</panel>
</layer>
</screen>
</nifty>
[/java]

[java]
public class Game extends SimpleApplication implements ScreenController, Controller{

@Override
public void simpleInitApp() {
System.out.println("simpleInitApp() "+app);
startScreenState = new StartScreenState(app, nifty, screen, popupElement, stateManager);
// startScreenState.myConstructor(this);
startNifty();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
flyCam.setDragToRotate(true);
// bulletAppState.getPhysicsSpace().enableDebug(assetManager); //shows phyiscs grid
setDisplayFps(false); // to hide the FPS
setDisplayStatView(false); // to hide the statistics
}

private void startNifty(){
    niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
    nifty = niftyDisplay.getNifty();

//nifty.fromXml() generates a Nifty Screen out of a XML file
nifty.fromXml(“Interface/screen.xml”, “startScreen”,this);
//guiViewPort.addProcessor shows the nifty predefined object on the display
guiViewPort.addProcessor(niftyDisplay);
//colores the panels of the Nifty Screen, makes it easy to find failures
nifty.setDebugOptionPanelColors(false);
inputManager.setCursorVisible(false);
}
[/java]

Does anyone have an idea why this works on desktop but not on android?
or example code how to use nifty on android would be great !

Greetings from switzerland

I tried finding the failure …

It seems it reacts on the button, but throws an Exception.
Any idea why this happens?

I/System.out(13248): loadGame: 0 //System.Out.println("loadGame: "+number); W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING Exception: java.lang.reflect.InvocationTargetException W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING java.lang.reflect.Method invokeNative (Method.java:-2) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING java.lang.reflect.Method invoke (Method.java:515) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:91) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:232) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:296) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty update (Nifty.java:288) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.input.InputManager processQueue (InputManager.java:819) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.input.InputManager update (InputManager.java:883) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.app.Application update (Application.java:604) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.app.SimpleApplication update (SimpleApplication.java:231) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.app.AndroidHarness update (AndroidHarness.java:550) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.system.android.OGLESContext onDrawFrame (OGLESContext.java:347) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING android.opengl.GLSurfaceView$GLThread guardedRun (GLSurfaceView.java:1523) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING android.opengl.GLSurfaceView$GLThread run (GLSurfaceView.java:1240) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING Root Cause: java.lang.NullPointerException W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING mygame.StartScreenState loadGameSave (StartScreenState.java:61) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING java.lang.reflect.Method invokeNative (Method.java:-2) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING java.lang.reflect.Method invoke (Method.java:515) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:91) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:232) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:296) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING de.lessvoid.nifty.Nifty update (Nifty.java:288) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.input.InputManager processQueue (InputManager.java:819) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.input.InputManager update (InputManager.java:883) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.app.Application update (Application.java:604) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.app.SimpleApplication update (SimpleApplication.java:231) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.app.AndroidHarness update (AndroidHarness.java:550) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING com.jme3.system.android.OGLESContext onDrawFrame (OGLESContext.java:347) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING android.opengl.GLSurfaceView$GLThread guardedRun (GLSurfaceView.java:1523) W/de.lessvoid.nifty.NiftyMethodInvoker(13248): WARNING android.opengl.GLSurfaceView$GLThread run (GLSurfaceView.java:1240)

NullPointerException at:
WARNING mygame.StartScreenState loadGameSave (StartScreenState.java:61)

Seems like it may be related to 90% of all problem posts related to nifty. If so then you have an issue with how you’ve given the screen state to nifty.

1 Like

Ok I found the failure, stupid failure …

I made an instance of app in my class, the problem was now that main method is executed on desktop but not on android.
So the Game app was always null and of course it didn’t react, if it isn’t there …

[java]
public class Game extends SimpleApplication implements ScreenController, Controller {
static Game app;

public static void main(String[] args) {
    app = new Game();
    app.start();
}

[/java]

Iam initialising in simpleInitApp() now, anyone know is there a better place for this or is simpleInitApp() ok for that?
[java]
//end attributes
@Override
public void simpleInitApp() {
if(app==null){
app=new Game();
}
System.out.println("simpleInitApp() "+app);
startScreenState = new StartScreenState(app, nifty, screen, popupElement, stateManager);
startNifty();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
flyCam.setDragToRotate(true);
// bulletAppState.getPhysicsSpace().enableDebug(assetManager); //shows phyiscs grid
setDisplayFps(false); // to hide the FPS
setDisplayStatView(false); // to hide the statistics

}

[/java]

@pspeed
How did you see from this code that it is an nullpointer?

@hello123 said: @pspeed How did you see from this code that it is an nullpointer?

I read the log you provided.

…no magic involved.

Note: a properly designed app shouldn’t need to do this:
static Game app;

…it’s kind of a cheat/hack to get around design problems, really.

I saw that one, but thought it won’t be a problem because its the Root node.
Didn’t thought the whole class isn’t there.

True
I thought before I run into several problems by bad object orientating, I first start with a static class.
Later as soon as I know how all keeps together, give the fields to all classes and make it object orientated.

New question
Does nifty work well on android as hud?

cause got a new problem with it. As allways work well on desktop.
On android my hud appears on the screen but what should be transparent is still black.
Sometimes I can see some parts of the game for seconds.

Anyone has an android example what i could use to figure out how to make a working hud?

Kind regards
Alex

@hello123 said: New question Does nifty work well on android as hud?

Yes, it works fine for me. I use image buttons as HUD. Works exactly like the desktop version.
Better post your example or a reduced example…

1 Like

OK
Still trying to find this failure. I setup a new project and copy/paste piece for piece to find out why this happens.

Give me a sec, I make screenshots first.

@hello123 said: New question Does nifty work well on android as hud?

cause got a new problem with it. As allways work well on desktop.
On android my hud appears on the screen but what should be transparent is still black.
Sometimes I can see some parts of the game for seconds.

Anyone has an android example what i could use to figure out how to make a working hud?

Kind regards
Alex

Does your phone have an adreno gpu? If the last object drawn turns off depth writing, then clearing the depth buffer doesn’t work properly. You need to explicitly turn on depth writing after everything is rendered. I do it in OGLESShaderRender.java calling GLES20.glDepthMask(true); right before GLES20.glClear(bits).

1 Like

Desktop Picture:
Desktop_picture_link

Android Picture
Android_picture_link

Android_picuture_link2

Must be a problem with my implementation of nifty than.
Sry right now I have no idea what piece of code I should paste…

Wtf is adreno O-o ?
Dunno have to ask google first. Phone is a Sony Compact.

Uf never played with shader until now …
Can it easyli be switched on in harness?

Hmm the weird failure has disappeared…

It seems that I have fixed it some how while searching for it… still quite strange

Grafic problem camed back again.

Found out as soon as I do:

[java]
setDisplayFps(false);
setDisplayStatView(false);
[/java]

this problem appears

If I implement 1 of those 2 methods it runs well.
Anyone an idea?

If u wana look into my code, paste or pm your email. I will send you a zip file with the content.

Kind regards :slight_smile:
Alex

Maybe try your code first with another device? An emulator could also do the job.

While using NiftyGUI with Android unable to see the button only text appears but works fine in desktop. Why does it happens?

Hmm, it used to work fine for me, but that was some time ago.

1 Like