help......i am a Beginners

by yoyo » Tue, 14 Apr 2009 00:58:56 GMT


Sponsored Links
 package com.androidyo.firstbmi;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Bmi extends Activity {

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button button=(Button)findViewById(R.id.submit);
        button.setOnClickListener(calcBMI);
    }
    private OnClickListener calcBMI = new OnClickListener()
    {
        public void onClick(View v)
        {
                DecimalFormat nf=new DecimalFormat("0.00");

                EditText fieldheight=(EditText)findViewById(R.id.height);
                EditText fieldweight = (EditText)findViewById(R.id.weight);
                double height = 
Double.parseDouble(fieldheight.getText().toString
());
                height=height/100;
                double weight = 
Double.parseDouble(fieldweight.getText().toString
());

                TextView result = (TextView)findViewById(R.id.result);
                TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
                if(height<=0||weight<=0)
                        result.setText("input wrong");
                else
                {
                   double BMI = weight / (height * height);


             result.setText("Your BMI is "+nf.format(BMI));

             if(BMI>25){
                    fieldsuggest.setText(R.string.advice_heavy);
                   }else if(BMI<20){
                fieldsuggest.setText(R.string.advice_light);
                   }else{
                    fieldsuggest.setText(R.string.advice_average);
                   }
             }

        }

    };
}



MY question:



when runing the app .if i input nothing .it will force close

how can i fix it???
please give me a hang...i am a beginners.thanks

--~--~---------~--~----~------------~-------~--~----~



help......i am a Beginners

by Marco Nelissen » Tue, 14 Apr 2009 01:10:14 GMT


 You almost no information as to what goes wrong, but my guess is that
since you didn't input any values, the call to Double.parseDouble()
throws an exception.
BTW, since you say you're a beginner, you might want to give the
android-beginners mailing list a try.





--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


help......i am a Beginners

by MrChaz » Tue, 14 Apr 2009 01:22:03 GMT


 You need to make sure that there is text in the TextEdits before
calling parseDouble because it will throw an exception otherwise.
Something like:

String heightText = fieldheight.getText().toString();
if( heightText  != null && !heightText.equals("")){
 // do stuff here
}

should work.



--~--~---------~--~----~------------~-------~--~----~