获得"无限"来自android上的textfield的值

时间:2014-11-25 17:38:28

标签: android android-fragments textfield calculator infinity

我在片段中制作一个简单的bodyfat计算器,我在运行应用程序时获得无限值,然后我试着看看它是否从变量体重得到0值,实际上,得到0值,我是新的片段,也许它有问题吗?

这是代码

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;
    }
}

1 个答案:

答案 0 :(得分:1)

使用此:

Integer.parseInt(etBodyWeight.getText().toString());

而不是:

Integer.parseInt(etBodyWeight.toString());

另请参阅答案here for getting value from EditText 同时更改为etWaist.getText().toString()