jeremy.83
19-09-2008, 20:34
Ciao, ho iniziato a smanettare col J2ME, per un esame.
Ho un problema con la comprensione dei Command, che dovrebbero essere facili, e infatti non riesco a trovare l'errore.
Ad esempio questo stupidissimo codice:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Interests extends MIDlet implements CommandListener{
private Form formMain, formResults;
private Command cmdExit, cmdBack, cmdGO;
private ChoiceGroup categoria, categoria2;
public Interests(){
init();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
notifyDestroyed();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(formMain);
}
private void getResults(){
categoria2 = new ChoiceGroup("Scegli la/le categoria/e di tuo interesse", Choice.MULTIPLE, new String[]{"Ristorante", "Hotel", "Museo", "Cinema", "Teatro"}, null);
formResults.append(categoria2);
formResults.addCommand(cmdBack);
Display.getDisplay(this).setCurrent(formResults);
}
private void init(){
categoria = new ChoiceGroup("Scegli la/le categoria/e di tuo interesse", Choice.MULTIPLE, new String[]{"Ristorante", "Hotel", "Museo", "Cinema", "Teatro"}, null);
cmdExit = new Command("Exit", Command.EXIT, 1);
cmdBack = new Command("Back", Command.BACK, 2);
cmdGO = new Command ("Vai!", Command.OK, 3);
formMain.append(categoria);
formMain.addCommand(cmdExit);
formMain.setCommandListener(this);
formResults.setCommandListener(this);
}
public void commandAction(Command c, Displayable s){
if (c == cmdExit)
notifyDestroyed();
if (c == cmdGO)
getResults();
if (c == cmdBack)
Display.getDisplay(this).setCurrent(formMain);
if (s == formResults && c == cmdBack)
notifyDestroyed();
}
}
mi solleva l'eccezione "java.lang.NullPointerException".
Mi sembra che la sollevi quando lancio il metodo setCommandListener.
C'è da mettere un try catch? Sì ma dove?:confused:
La cosa strana è che in altri esempi che ho io un po' più complessi questa eccezione non viene sollevata.
Scusate per la noobbaggine :D
:help:
Ho un problema con la comprensione dei Command, che dovrebbero essere facili, e infatti non riesco a trovare l'errore.
Ad esempio questo stupidissimo codice:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Interests extends MIDlet implements CommandListener{
private Form formMain, formResults;
private Command cmdExit, cmdBack, cmdGO;
private ChoiceGroup categoria, categoria2;
public Interests(){
init();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
notifyDestroyed();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(formMain);
}
private void getResults(){
categoria2 = new ChoiceGroup("Scegli la/le categoria/e di tuo interesse", Choice.MULTIPLE, new String[]{"Ristorante", "Hotel", "Museo", "Cinema", "Teatro"}, null);
formResults.append(categoria2);
formResults.addCommand(cmdBack);
Display.getDisplay(this).setCurrent(formResults);
}
private void init(){
categoria = new ChoiceGroup("Scegli la/le categoria/e di tuo interesse", Choice.MULTIPLE, new String[]{"Ristorante", "Hotel", "Museo", "Cinema", "Teatro"}, null);
cmdExit = new Command("Exit", Command.EXIT, 1);
cmdBack = new Command("Back", Command.BACK, 2);
cmdGO = new Command ("Vai!", Command.OK, 3);
formMain.append(categoria);
formMain.addCommand(cmdExit);
formMain.setCommandListener(this);
formResults.setCommandListener(this);
}
public void commandAction(Command c, Displayable s){
if (c == cmdExit)
notifyDestroyed();
if (c == cmdGO)
getResults();
if (c == cmdBack)
Display.getDisplay(this).setCurrent(formMain);
if (s == formResults && c == cmdBack)
notifyDestroyed();
}
}
mi solleva l'eccezione "java.lang.NullPointerException".
Mi sembra che la sollevi quando lancio il metodo setCommandListener.
C'è da mettere un try catch? Sì ma dove?:confused:
La cosa strana è che in altri esempi che ho io un po' più complessi questa eccezione non viene sollevata.
Scusate per la noobbaggine :D
:help: