Fail to run the J2ME program after I appended the checkbox's index
Why the program is failed开发者_JAVA百科 to run after I appended the checkbox's index. agreeIndex = mainForm.append(agree);
import javax.microedition.midlet.*;
import java.util.*;
import javax.microedition.lcdui.*;
public class MainPage extends MIDlet implements CommandListener, ItemStateListener{
Ticker header;
Display display;
Form mainForm;
TextField name, num, phone, email, age;
ChoiceGroup gender;
private int choiceGroupIndex;
Command nextCom, clearCom, exitCom;
Page1 p1;
ChoiceGroup agree = new ChoiceGroup("Please read the terms and conditions from menu."
, Choice.MULTIPLE);
int agreeIndex;
public MainPage(){
header = new Ticker("**ABC Restraurant**Customer Survey**");
mainForm = new Form("Personal Information");
name = new TextField("User name: ", "", 20, TextField.ANY);
num = new TextField("User id: ", "", 10, TextField.NUMERIC);
phone = new TextField("Phone no.: ", "", 10, TextField.PHONENUMBER);
email = new TextField("E-mail: ", "", 20, TextField.ANY);
age = new TextField("Age: ", "", 3, TextField.NUMERIC);
gender = new ChoiceGroup("Gender: ", Choice.EXCLUSIVE);
nextCom = new Command("Next", Command.SCREEN, 1);
clearCom = new Command("Clear", Command.SCREEN, 2);
exitCom = new Command("Exit", Command.EXIT, 3);
p1 = new Page1(this);
mainForm.setTicker(header);
mainForm.append(name);
mainForm.append(num);
gender.append("Male", null);
gender.append("Female", null);
choiceGroupIndex = mainForm.append(gender);
mainForm.append(age);
mainForm.append(phone);
mainForm.append(email);
//agree.append("I agree with the terms and conditions.", null);
mainForm.append(agree);
mainForm.addCommand(nextCom);
mainForm.addCommand(clearCom);
mainForm.addCommand(exitCom);
mainForm.setCommandListener(this);
}
public void commandAction(Command c, Displayable s) {
if (c == exitCom)
{
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
notifyDestroyed();
} else if (c == clearCom) {
resetTransferInformation();
} else if (c == nextCom) {
display.setCurrent(p1.list);
//System.out.print(agreeIndex);
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(mainForm);
agree.append("I agree with the terms and conditions.", null);
agreeIndex = mainForm.append(agree);
}
public void itemStateChanged(Item item)
{
}
public void resetTransferInformation() {
name.setString("");
num.setString("");
phone.setString("");
email.setString("");
age.setString("");
}
public void back() {
display.setCurrent(mainForm);
}
}
You try to append agree to mainForm twice. (in constructor and in startApp)
It's incorrect
精彩评论