2

我有一个表单中的 TextField:

private TextField cc1F = new TextField("", "", 250, TextField.ANY);

单击 OK 按钮后,将 cc1F 字段的约束设置为:

cc1F.setConstraints(TextField.UNEDITABLE);

然后我得到一个例外:

java.lang.NullPointerException
at com.sun.midp.lcdui.DefaultInputMethodHandler.setConstraints(+63)
at javax.microedition.lcdui.TextField$InputMethodClientImpl.setConstraints(+20)
at javax.microedition.lcdui.TextField.setConstraints(+37)
at com.zousys.j2me.zmail.gui.view.HeaderView.menuAction(+146)

.....................................

有人知道吗?

4

1 回答 1

0

代码示例使用模拟器 WTK 3.0 和设备 Samsung Chat http://www.gsmarena.com/samsung_ch@t_527-4096.php测试,测试通过。:)

我认为问题是由设备型号或对象cc1F为空引起的。

import java.io.UnsupportedEncodingException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class TextFieldMIDlet extends MIDlet implements CommandListener {

    private Form form;
    private Command cmdExit;
    private Command cmdSetConstraints;
    private Display display;
    private TextField textField;

    public TextFieldMIDlet() {
        form = new Form("Testing constraints");

        cmdExit = new Command("Exit",Command.EXIT, 1);
        cmdSetConstraints = new Command("Set Constraints",Command.ITEM, 1);
        textField = new TextField("", "", 250, TextField.ANY);

        form.addCommand(cmdExit);
        form.addCommand(cmdSetConstraints);
        form.append(textField);

        display = Display.getDisplay(this);
        form.setCommandListener(this);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {

        display.setCurrent(form);

    }

    public void commandAction(Command c, Displayable d) {

        if (c == cmdExit) {

            this.notifyDestroyed();

        } else if (c == cmdSetConstraints) {

            try {
                System.out.println("Set...");

                if (textField.getConstraints() == TextField.UNEDITABLE) {
                    textField.setConstraints(TextField.ANY );
                } else {
                    textField.setConstraints(TextField.UNEDITABLE );
                }


            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}
于 2012-07-20T12:29:59.870 回答