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.

On Apr 13, 3:11 pm, yoyo <zjw...@gmail.com> wrote:
> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to