Results 1 to 6 of 6
  1. #1
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default 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
    Java 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
    Java 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);
        }
    
    
    }

  2. #2
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Solved it :P

    you need to create the textfields everytime the method is called... thanks

    Java 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);
            height  = new TextField("Height:", "6", 30, TextField.ANY);
            weight = new TextField("Weight:", "150", 30, TextField.ANY);
            
            form.append(TheChoice);
            form.append(height);
            form.append(weight);
            form.addCommand(cancel);
            form.addCommand(submit1);
            form.setCommandListener(this);
            display.setCurrent(form);
        }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by kev670 View Post
    I get an error
    In future it helps if you post the EXACT error message. We don't read minds.

  4. #4
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    The error was non descriptive.
    My post was... sorry for wasting your time

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    No, errors are very informative. What you need to do is learn what information they are providing. If you do not understand them then you post them on a forum and ask more experienced people.

  6. #6
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    No, errors are very informative. What you need to do is learn what information they are providing. If you do not understand them then you post them on a forum and ask more experienced people.
    Ok, cheers

Similar Threads

  1. Splash screen problem
    By goklien in forum NetBeans
    Replies: 1
    Last Post: 03-22-2011, 08:30 AM
  2. Replies: 3
    Last Post: 02-04-2011, 06:30 AM
  3. screen resolution problem
    By prabhug1987 in forum Advanced Java
    Replies: 1
    Last Post: 10-15-2010, 07:01 PM
  4. Full Screen Frame Resolution Problem
    By Krooger in forum AWT / Swing
    Replies: 2
    Last Post: 11-24-2009, 11:05 PM
  5. how to Store and recall numbers in java ?
    By Ak-Emm in forum New To Java
    Replies: 16
    Last Post: 09-28-2008, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •