Android 下拉菜单不显示选项值结果

3

我正在尝试计算女性的BMR,但是旋转器出现错误:

09-01 22:08:52.373: E/AndroidRuntime(344):  at com.fps.iHealthFirst.BMRCalculator.onClick(BMRCalculator.java:286)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.view.View.performClick(View.java:2485)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.view.View$PerformClick.run(View.java:9080)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.os.Handler.handleCallback(Handler.java:587)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.os.Looper.loop(Looper.java:123)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-01 22:08:52.373: E/AndroidRuntime(344):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 22:08:52.373: E/AndroidRuntime(344):  at java.lang.reflect.Method.invoke(Method.java:507)
09-01 22:08:52.373: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-01 22:08:52.373: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-01 22:08:52.373: E/AndroidRuntime(344):  at dalvik.system.NativeStart.main(Native Method)

以下是我的代码,试图获取静坐、轻度运动、中度运动和重度运动的旋钮值:

private void calculateWomen(){

    String f1a = etft.getText().toString();
    String f2a = etin.getText().toString();
    String f3a = etweight.getText().toString();
    String f4a = etage.getText().toString();

    if ( ( f1a.isEmpty() || f2a.isEmpty() || f3a.isEmpty() || f4a.isEmpty() ) ) 
        // call for custom toast
        viewErrorToast();
    else{
    // Metric Formula for BMR (Women) English Unit
    //655 + ( 4.3379 x weight in pounds ) + 
    //( 4.6980 x height in inches ) - ( 4.6756 x age in years )
     String age, in, ft, weight;
     Double answer;

     age =  etage.getText().toString();
     in =  etin.getText().toString();    
     ft = etft.getText().toString();
     weight = etweight.getText().toString();


        if (spinnerText.equals("Sedentary")){
            //actAnswer = "1.2";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.2);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }
        else if (spinnerText.equals("Lightly Active")){
            //actAnswer = "1.375";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.375);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }
        else if (spinnerText.equals("Moderately Active")){
            // actAnswer = "1.55";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.55);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }
        else if (spinnerText.equals("Heavily Active")){
            // actAnswer = "1.725";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.725);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }


     // call for custom toast
     viewBMRSavedToast();
    }

} // end of calculateWomen method

如果您注意到任何问题,请通知我,谢谢。非常感激。

1
这是所有的logcat错误吗?如果是,那么你的错误在onClick(View v)方法中而不是calculateWomen()。请发布您的onClick()方法并指出第286行:"at com.fps.iHealthFirst.BMRCalculator.onClick(BMRCalculator.java:286)"。 - Sam
2个回答

3
您可以使用这种方法:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

如果(spinnerText.equals(“久坐”)){ // “1.2” 如果(男性.isChecked()) { 答案 =((66 +(6.2377 * Double.parseDouble(weight))+ (12.7084 (Double.parseDouble(in) 12 + Double.parseDouble(ft))) -(6.8 * Double.parseDouble(age)))* 1.2); BigDecimal bd = BigDecimal.valueOf(answer); bd = bd.setScale(2,BigDecimal.ROUND_FLOOR); etanswer.setText(bd.toString()); } - Dunkey
当用户点击计算按钮时,应该显示结果。计算不应放置在setOnItemSelectedListener中...那么我可以把它放在哪里? - Dunkey

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接