使用是,否命令显示警报 [英] Displaying Alert with Yes, No Command

查看:97
本文介绍了使用是,否命令显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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中。除此之外,您发布的代码段中没有明显的错误。

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.

该错误很可能是由于您的<$ c $错误导致的c> getForm()方法代码,但是由于没有日志记录,因此您还必须检查其他可能性,例如该命令侦听器或 no 命令对象或警报对象已在代码中的其他地方进行了更改。

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.

日志记录如下例所示,您只需在模拟器中运行您的Midlet并检查控制台消息,以查看是否已执行了预期的代码:

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...
    }
}

这篇关于使用是,否命令显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆