Choisebox - Wie kann ich den SelectedIndex ausgeben?

Status
Nicht offen für weitere Antworten.

xip

Bekanntes Mitglied
Hallo,

denke mal ich habe hier eine wirklich leichte Frage. Aber ich komme nicht drauf. Die Funktion getSelectedIndex() wirft bei mir nur Exceptions.

Hier meine Code:

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloMIDlet extends MIDlet implements CommandListener {

private Command exitCommand; // The exit command
private Display display; // The display for this MIDlet
private Form form = new Form("Choice 1");
private Command OKCommand;
private ChoiceGroup choiceGroup;

public HelloMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 0);
OKCommand = new Command("OK",Command.OK,0);
}

public void startApp()
{
choiceGroup = new ChoiceGroup( "Bitte zuerst Farben wählen", Choice.EXCLUSIVE, new String[] { "rot", "grün", "blau" }, null );
choiceGroup.setDefaultCommand(OKCommand);

form.append(choiceGroup);
form.addCommand(exitCommand);
form.setCommandListener(this);

display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
if (c == OKCommand) {
System.out.println( choiceGroup.getSelectedIndex() );
}
}
}


Und Die Exception:

TRACE: <at java.lang.NullPointerException: 0>, Exception caught in Display class
java.lang.NullPointerException: 0
- javax.microedition.lcdui.Display$ChameleonTunnel.callItemListener(), bci=57
- com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=57
- com.sun.midp.chameleon.layers.SoftButtonLayer.soft2(), bci=173
- com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=78
- com.sun.midp.chameleon.CWindow.keyInput(), bci=38
- javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
- com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
- com.sun.midp.events.EventQueue.run(), bci=179
- java.lang.Thread.run(), bci=11

Habt ihr da ein Idee?

lg
 
Zuletzt bearbeitet:

CNail187

Mitglied
Hallo!

Also wenn ich deinen Code 1:1 kopiere passiert bei mir gar nix...

Mit folgender Änderung:

Java:
//choiceGroup.setDefaultCommand(OKCommand);
form.addCommand(OKCommand);

wird der selectedIndex einwandfrei ausgegeben.
Welchen Emulator benutzt du?

Grüße,
CNail
 

xip

Bekanntes Mitglied
whow,

habe nur das gemacht was du mir vorgeschlagen hast, und jetzt funktionierts!!!

Dank dir!!
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben