【问题标题】:Getting "infinity" value from textfield on android从android上的文本字段获取“无穷大”值
【发布时间】:2014-11-25 17:38:28
【问题描述】:

我在片段中制作了一个简单的体脂计算器,在运行应用程序时我得到了无穷大的值,然后我试图看看它是否从变量体重中得到一个 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;
    }
}

【问题讨论】:

    标签: android android-fragments textfield calculator infinity


    【解决方案1】:

    使用这个:

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

    代替:

    Integer.parseInt(etBodyWeight.toString());
    

    另见答案here for getting value from EditText
    也改成etWaist.getText().toString()

    【讨论】:

    • 哦,谢谢,解决了问题嘿嘿,我现在感觉很笨,谢谢mane,是啊,我好久没用android了,我忘了,谢谢。
    • @GregorioMerazJr。另外,如果出现NumberFormatException,结果将毫无意义,因此您应该显示错误消息并且不要继续计算
    • 哦,谢谢,您的意思是在发生错误时显示祝酒词?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多