2011-03-17 57 views
2

在J2me应用程序中,我使用了yes,no命令的警报。如果用户点击yes命令,屏幕将会显示,如果点击no命令,TextBox屏幕将会显示。但是代码不起作用。对于两个命令,仅显示文本框屏幕。使用是,无命令显示警报

这是我的代码:

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

哪里是我的错吗?

+0

是什么项目? – 2011-03-17 23:03:04

+0

是其他类,继承了那个类的形式。我简化了我的代码并忘记了这一行。我编辑它 – 2011-03-17 23:13:15

回答

-3

您在这里犯的主要错误是我认为您的MIDlet中没有使用合适的logging。除此之外,您发布的代码段中没有明显的错误。

这是最有可能的是,错误的东西在你的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... 
    } 
}