im making a simple bodyfat calculator inside a fragment, im getting infinity value when running the app, then i tried to see if it was getting a 0 vale from the variable bodyweight and indeed, is getting a 0 value, im new on fragments, maybe something is wrong with it?
here is the code
public class GamesFragment extends Fragment {
EditText etBodyWeight, etWaist;
Button btnCalculate;
TextView tvResult;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
etBodyWeight=(EditText)rootView.findViewById(R.id.tfBodyWeight);
etWaist=(EditText)rootView.findViewById(R.id.tfWaist);
tvResult=(TextView)rootView.findViewById(R.id.tvResult);
btnCalculate=(Button)rootView.findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(new OnClickListener() {
int bodyweight, waist;
Double factor1, factor2, lbm, bfw, bfp;
String result;
@Override
public void onClick(View v) {
try{
bodyweight=Integer.parseInt(etBodyWeight.toString());
}
catch(NumberFormatException e){
System.out.println("numero no valido");
}
factor1=(bodyweight*1.082)+94.42;
try{
waist=Integer.parseInt(etWaist.toString());
}
catch(NumberFormatException e){
System.out.println("numero no valido");
}
factor2=waist*4.15;
lbm=factor1-factor2;
bfw=bodyweight-lbm;
bfp=(bfw*100)/bodyweight;
result=Double.toString(bfp);
tvResult.setText(result);
}
});
return rootView;
}
}
Use this:
instead of:
Also see the answer here for getting value from EditText.
Also change to
etWaist.getText().toString()
.