开发者

Displaying Alert with Yes, No Command

开发者 https://www.devze.com 2023-02-17 17:46 出处:网络
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 d

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)
    {
     开发者_如何学Python   if(command==no)
        {
            switchDisplayable(alert,getForm());
        }
        else if(command==yes)
        {
            switchDisplayable(alert,getTextbox());
        }
    }
}

Where is my fault?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号