Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

java me - Displaying Alert with Yes, No Command

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...