Вопрос

I wanted to make it so that when I select a specific TextField in a Form, I get a specific list of Commands,

for example
TextField: OK, Cancel
TextField2: OK, Back, Help

And I succeeded by using addCommand() on each of TextFields but I can't tell it what to happen when when I activate one of these commands since there is no setCommandListener method for them.

It throws a NullPointerException when I click it.

I also tried to get the commands from their Form but it is receiving only the commands that it contains, not the commands that TextFields have.

How to get around this?

Это было полезно?

Решение

TextField is an Item object, and to use commands with it you need ItemCommandListener:

A listener type for receiving notification of commands that have been invoked on Item objects. An Item can have Commands associated with it. When such a command is invoked, the application is notified by having the commandAction() method called on the ItemCommandListener that had been set on the Item with a call to setItemCommandListener()...

Most likely reason for NullPointerException you mentioned is that you did not set appropriate item command listener (it's null by default, thus when MIDP attempts to invoke command action, there is NPE).

To get what you want, besides creating and adding commands, you need to create item command listener, implement code to handle commands in it, then associate your text field with this listener:

myTextField.setItemCommandListener(myItemCommandListener);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top