1

在 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());
        }
    }
}

我的错在哪里?

4

1 回答 1

-2

您的主要错误是我认为没有在您的 MIDlet 中使用适当的日志记录。除此之外,您发布的代码片段中没有明显的错误。

该错误很可能是由您的getForm()方法代码中出现问题引起的,但由于没有日志记录,您还必须检查其他可能性,例如命令侦听器或no命令对象,或者alert对象已在其他地方以某种方式更改你的代码。

使用如下示例所示的日志记录,您可以简单地在模拟器中运行 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...
    }
}
于 2012-12-18T11:48:46.680 回答