J2ME(LWUIT) - actionPerformed未调用

时间:2011-12-22 14:29:47

标签: java-me lwuit

我有两个LWUIT Form s(主密码和更改密码)
一个人使用actionPerformed呼叫另一个并且它有效 然后,在第二个,我需要获得一些数据,处理并返回到第一个 为此,我尝试再次使用actionPerformed。但是Ok按钮(手机上的右键),不会调用更改密码actionPerformed的{​​{1}}。只需致电主Form的{​​{1}}即可。为什么呢?

在代码的其他部分,我也这样做,但使用actionPerformed且只有一个Form并且它可以正常工作。

有什么可以改变吗? 我在测试代码时失去了很多时间,但我找不到解决方案

这是“主要”代码MIDlet

Form

这是更改密码的代码Form

public class InfoView extends Form implements ActionListener{

    private Command backCommand;
    private Command changePasswordCommand;
    private Command okChangePasswordForm;

     private ChangePassword changePasswordForm;

    public InfoView(Command backC){

        super();

        this.addCommand(backC);
        this.setBackCommand(backC);

        this.setScrollableY(true);

        this.setTitle(LanguageManager.getText("RootTitle"));

        changePasswordCommand = new Command(LanguageManager.getText("ChangePassword"), Constants.CHANGE_PASSWORD_COMMAND);

        okChangePasswordForm = new Command(LanguageManager.getText("Ok"), Constants.OK_CHANGE_PASSWORD_COMMAND);

        this.addAllCommands();       

        this.initForm();
    }

    public void initForm(){

        Style s = this.getStyle();

        s.setMargin(0, 0, 0, 0);
        s.setPadding(0, 0, 0, 0);

        this.addCommandListener(this);

        backCommand = new Command(LanguageManager.getText("Back"), Constants.BACK_COMMAND);

    }

    private void addAllCommands(){

        this.addCommand(changePasswordCommand);

        this.addCommand(changeContactInfoCommand);
    }

    public void actionPerformed(ActionEvent arg0) {

        //Obtengo la Opción seleccionada
        Command cmd = arg0.getCommand();

        if (cmd == null) {
            return;
        }

        System.out.println(String.valueOf(cmd.getId()));

        switch (cmd.getId()) {

            case Constants.CHANGE_PASSWORD_COMMAND:

                System.out.println("CHANGE_PASSWORD_COMMAND");

                //this.setGlassPane(null);

                if (changePasswordForm == null) {
                    changePasswordForm = new ChangePassword();
                    changePasswordForm.addCommand(backCommand);
                    changePasswordForm.addCommand(okChangePasswordForm);
                    changePasswordForm.addCommandListener(this);
                    changePasswordForm.setBackCommand(backCommand);
                }

                changePasswordForm.show();
                arg0.consume();
                break;

            case Constants.BACK_COMMAND:

                System.out.println("BACK_COMMAND");

                //20111004 MB: Cuando vuelvo, desincronizo para que al 
                //cambiar de tarjeta funcione
                //protocolManager.deSync();

                addAllCommands();

                this.show();

                this.editInfoForm = null;
                this.changePasswordForm = null;

                System.gc();

                break; 

            case Constants.OK_CHANGE_PASSWORD_COMMAND:

                System.out.println("OK_CHANGE_PASSWORD_COMMAND");

                this.editInfoForm = null;

                this.show();

                System.gc();

                break;                
        }
    }
}

1 个答案:

答案 0 :(得分:2)

知道了!

InfoView.actionPerformed(...),在Constants.CHANGE_PASSWORD_COMMAND:构造函数的ChangePassword()下, ChangePassword.addCommandListener(...)changePasswordForm.addCommandListener(this);覆盖因此当显示ChangePassword表单时,InfoView.actionPerformed(...)的keyEvents处于活动状态。

代码段#1

...
case Constants.CHANGE_PASSWORD_COMMAND:
    changePasswordForm = new ChangePassword();
    changePasswordForm.addCommand(backCommand);
    changePasswordForm.addCommandListener(this); //<-- PROBLEM, because 'this' 
                                                 //          is referring to
                                                 //          'InfoView' and NOT
                                                 //          'ChangePassword'
    changePasswordForm.setBackCommand(backCommand);
...

代码段#2

public ChangePassword(){
 ...
 addCommandListener(this);   //<-- Being over-written
 ...
}

解决方案只是评论或删除上面代码段#1中标记的问题语句。

我还建议在课程backCommand中创建ChangePassword,以便InfoView.backCommand不会隐藏它。