Java me 使用是、否命令显示警报

Java me 使用是、否命令显示警报,java-me,alert,midp,lcdui,Java Me,Alert,Midp,Lcdui,在J2me应用程序中,我使用了带有yes,no命令的警报。如果用户单击“是”命令表单屏幕将显示,如果单击“否”命令文本框屏幕将显示。但代码不起作用。对于两个命令,只显示文本框屏幕 这是我的代码: public Login(){ yes=new Command("Yes",Command.OK,1); no=new Command("No",Command.CANCEL,1); alert=new Alert("","Save The Changes?",null,Aler

在J2me应用程序中,我使用了带有yes,no命令的警报。如果用户单击“是”命令表单屏幕将显示,如果单击“否”命令文本框屏幕将显示。但代码不起作用。对于两个命令,只显示文本框屏幕

这是我的代码:

public Login(){
    yes=new Command("Yes",Command.OK,1);
    no=new Command("No",Command.CANCEL,1);
    alert=new Alert("","Save The Changes?",null,AlertType.CONFIRMATION);
    alert.setTimeout(Alert.FOREVER);
    alert.addCommand(yes);
    alert.addCommand(no);
    textbox.setCommandListener(this);
    alert.setCommanListener(this);
}
public void commandAction(Command command, Displayable displayable) {
    if(displayable==textbox)
    {
        if(command==exit)
        {
            switchDisplayable(null,alert);
        }
    }
    else if(displayable==alert)
    {
        if(command==no)
        {
            switchDisplayable(alert,getForm());
        }
        else if(command==yes)
        {
            switchDisplayable(alert,getTextbox());
        }
    }
}

我的错在哪里?

你的主要错误是我认为你的MIDlet使用不当。除此之外,您发布的代码片段中没有明显的错误

错误很可能是由
getForm()
方法代码中的错误引起的,但由于没有日志记录,您还必须检查其他可能性,例如命令侦听器或
no
命令对象,或
警报
对象在代码中的其他地方发生了更改

使用如下例所示的日志记录,您只需在emulator中运行midlet并检查控制台消息,以确定是否执行了预期的代码:

public void commandAction(Command command, Displayable displayable) {
    Log.log("command: [" + command.getCommandLabel()
            + "] at screen: [" + displayable.getTitle() + "]");
    if(displayable==textbox)
    {
        Log.log("in textbox");
        if(command==exit)
        {
            Log.log("handle exit command");
            switchDisplayable(null,alert);
        }
    }
    else if(displayable==alert)
    {
        Log.log("in alert");
        if(command==no)
        {
            Log.log("handle no command");
            switchDisplayable(alert,getForm());
        }
        else if(command==yes)
        {
            Log.log("handle yes command");
            switchDisplayable(alert,getTextbox());
        }
    }
}
//...


public class Log {
    // utility class to keep logging code in one place
    public static void log (String message) {
        System.out.println(message);
        // when debugging at real device, S.o.p above can be refactored
        //  - based on ideas like one used here (with Form.append):
        //    http://stackoverflow.com/questions/10649974
        //  - Another option would be to write log to RMS
        //    and use dedicated MIDlet to read it from there
        //  - If MIDlet has network connection, an option is
        //    to pass log messages over the network. Etc etc...
    }
}

是另一个类继承了这个类。我简化了代码,忘记了那一行。我编辑它