使用 Yes, No 命令显示警报

发布于 2024-10-23 18:11:42 字数 943 浏览 1 评论 0原文

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

我的错在哪里?

At the J2me application I used an alert with yes, no command. If user clicks the yes command Form Screen will be displayed and if clicks the no command TextBox screen will be displayed. But the code does not work. For two command only textbox screen will be displayed.

This is my code:

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

Where is my fault?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-10-30 18:11:42

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

该错误很可能是由 getForm() 方法代码中出现问题引起的,但由于没有日志记录,您还必须检查其他可能性,例如命令侦听器或 >没有命令对象或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...
    }
}

Your main fault here is I think not using appropriate logging in your MIDlet. Other than that, there are no evident mistakes in the code snippet you posted.

It is most likely that the error is caused by something going wrong in your getForm() method code, but since there is no logging, you have to also check other possibilities like eg that command listener or no command object, or alert object has been somehow changed somewhere else in your code.

With logging like shown in example below, you could simply run your midlet in emulator and check console messages to find out whether expected code has been executed or not:

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...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文