2011-03-17 2 views
2

J2ME 응용 프로그램에서 예, 아니요 명령으로 경고를 사용했습니다. 사용자가 yes 명령을 클릭하면 Form Screen이 표시되고 클릭하면 No command 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

항목 무엇입니까? –

+0

다른 클래스이며, 클래스를 상속받습니다. 내 코드를 단순화하고 그 라인을 잊어 버렸다. 그리고 나는 그것을 편집한다. –

답변

-3

여기에 가장 큰 실수는 MIDlet에 적절한 logging을 사용하지 않는 것입니다. 그 외에도 게시 한 코드 스 니펫에는 명백한 실수가 없습니다.

이 오류가 무언가가 당신의 getForm() 방법 코드에서 잘못된 것 때문에 발생하는 것이 대부분이지만, 로깅이 없기 때문에, 당신은 또한 청취자 또는 no 명령 개체, 또는 alert 개체를 명령 예를 들어 같은 다른 가능성을 확인해야 코드에서 어딘가 다른 곳으로 변경되었습니다. 로깅

은 다음 예에 표시된 것처럼, 당신은 단순히 에뮬레이터에서 미들 렛을 실행하고 예상 코드가 실행되었는지의 여부를 확인하기 위해 콘솔 메시지를 확인할 수 있습니다 :

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