2011-03-17 58 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... 
    } 
}