Java ME recall screen problem
I'm having trouble recalling a screen. I'm building a small BMI app. The idea is you enter your details and submit them, then you get your result and get 2 options... to cancel... or calc, when calc is called it calls the BMIMenu Method again to redraw to opening menu. The method works fine when it draws the opening screen but when i recall the method after calculating the BMI I get an error... can anyone help me. Thanks
This Method seems to be the main problem. If i delete form.append(height); and form.append(weight); then the program will work but i need those textboxes
Code:
public void BMIMenu()
{
display = Display.getDisplay(this);
form = new Form("Measurements?");
TheChoice = new ChoiceGroup("How Would you like to be measured:", Choice.EXCLUSIVE);
TheChoice.append("Feet / Pounds", null);
TheChoice.append("Metres / Kg's", null);
form.append(TheChoice);
int append = form.append(height);
form.append(weight);
form.addCommand(cancel);
form.addCommand(submit1);
form.setCommandListener(this);
display.setCurrent(form);
}
This is the whole program
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BMIMain extends MIDlet implements CommandListener
{
private Display display;
private TextField height, weight;
private Form form;
private ChoiceGroup TheChoice;
private Command cancel, submit1, calcMenu;
//------------------------------------------------
//Constructor
//------------------------------------------------
public BMIMain()
{
height = new TextField("Height:", "6", 30, TextField.ANY);
weight = new TextField("Weight:", "150", 30, TextField.ANY);
cancel = new Command("Cancel", Command.CANCEL, 0);
calcMenu = new Command("Calc", Command.OK, 0);
submit1 = new Command("Submit", Command.OK, 0);
}
//------------------------------------------------
//Start, Pause, Destroy
//------------------------------------------------
public void startApp() {BMIMenu();}
public void pauseApp() {}
public void destroyApp(boolean unconditional) { notifyDestroyed();}
//------------------------------------------------
// Layout Menus
//------------------------------------------------
public void BMIMenu()
{
display = Display.getDisplay(this);
form = new Form("Measurements?");
TheChoice = new ChoiceGroup("How Would you like to be measured:", Choice.EXCLUSIVE);
TheChoice.append("Feet / Pounds", null);
TheChoice.append("Metres / Kg's", null);
form.append(TheChoice);
int append = form.append(height);
form.append(weight);
form.addCommand(cancel);
form.addCommand(submit1);
form.setCommandListener(this);
display.setCurrent(form);
}
//------------------------------------------------
//------------------------------------------------
public void BMIResult(double BMI)
{
display = Display.getDisplay(this);
form = new Form("Your BMI?");
form.append("Your BMI is: " + BMI);
form.addCommand(cancel);
form.addCommand(calcMenu);
form.setCommandListener(this);
display.setCurrent(form);
}
//------------------------------------------------
//Action
//------------------------------------------------
public void commandAction(Command com, Displayable d)
{
String label = com.getLabel();
if(com == cancel)
{
destroyApp(true);
}
else if(com == calcMenu)
{
BMIMenu();
}
else if(com == submit1)
{
int a = TheChoice.getSelectedIndex();
BMICalc(a, height.getString(), weight.getString());
}
}
//------------------------------------------------
//Calculations
//------------------------------------------------
public void BMICalc(int choice, String h, String w)
{
double height = Double.valueOf(h).doubleValue();
double weight = Double.valueOf(w).doubleValue();
double BMI= 0.0;
if (choice == 0)
{
height = height * 12; // this converts it from feet to inches
BMI = (weight / (height * height)) * 703;
}
else if (choice == 1)
{
height = height * 12; // this converts it from feet to inches
BMI = weight / (height * height);
}
// this is to calculate BMI to 1 decimal place
BMI = BMI * 10;
int hold = (int)BMI;
BMI = (double)hold;
BMI = BMI / 10;
BMIResult(BMI);
}
}