1

J2me アプリケーションでは、yes, no コマンドでアラートを使用しました。ユーザーが yes コマンドをクリックするとフォーム画面が表示され、no コマンドをクリックすると TextBox 画面が表示されます。しかし、コードは機能しません。2 つのコマンドの場合のみ、テキストボックス画面が表示されます。

これは私のコードです:

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

私のせいはどこですか?

4

1 に答える 1

-2

ここでの主な欠点は、MIDlet で適切なログを使用していないと思います。それ以外は、投稿したコード スニペットに明らかな誤りはありません。

エラーはメソッド コードのどこかで問題が発生したことが原因である可能性が最も高いですが、ログが記録されていないため、コマンド リスナーやコマンド オブジェクト、またはオブジェクトがなんらかの方法で別の場所で変更されているなど、getForm()他の可能性も確認する必要があります。あなたのコード。noalert

以下の例に示すようなロギングを使用すると、エミュレーターで 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...
    }
}
于 2012-12-18T11:48:46.680 に答える