Java 单键安卓

Java 单键安卓,java,android,android-radiogroup,android-radiobutton,Java,Android,Android Radiogroup,Android Radiobutton,我正在开发一个简单的BMI计算器,由于我用单选按钮在代码中添加了性别的影响,AVD意外停止。问题是解释IMC功能,我相信radiobutton的ID没有读取,不知道为什么! 请帮我一点忙 package com.example.calculadorimc; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import androi

我正在开发一个简单的BMI计算器,由于我用单选按钮在代码中添加了性别的影响,AVD意外停止。问题是解释IMC功能,我相信radiobutton的ID没有读取,不知道为什么! 请帮我一点忙

package com.example.calculadorimc;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
    private RadioGroup rgsexo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void calculateClickHandler(View view) {

        // make sure we handle the click of the calculator button

        if (view.getId() == R.id.botaoCalcular) {

         // get the references to the widgets
         EditText editPeso = (EditText)findViewById(R.id.editPeso);
         EditText editAltura = (EditText)findViewById(R.id.editAltura);
         TextView imcView = (TextView)findViewById(R.id.imcView);

         // get the users values from the widget references

         float peso = Float.parseFloat(editPeso.getText().toString());
         float altura = Float.parseFloat(editAltura.getText().toString());

         // calculate the bmi value

         float imcValue = calcularIMC(peso, altura);

         // interpret the meaning of the bmi value
         String imcInterpretation = interpretIMC(imcValue);

         // now set the value in the result text

         imcView.setText(imcValue + "-" + imcInterpretation);
        }
       }
       // the formula to calculate the BMI index

       // check for http://en.wikipedia.org/wiki/Body_mass_index
       private float calcularIMC (float peso, float altura) {

        return (float) (peso / (altura * altura));
       }

       // interpret what BMI means
       private String interpretIMC(float imcValue) {           

           int selectedId = rgsexo.getCheckedRadioButtonId();  // get the id

           switch (selectedId)   // switch on the button selected
           {
                case R.id.radioMasc:
                    if (imcValue < 16) {
                        return "Magreza Grave";
                       } else if (imcValue < 18.5) {

                        return "Magreza Leve";
                       } else if (imcValue < 25) {

                        return "Saudável";
                       } else if (imcValue < 30) {

                        return "Sobrepeso";
                       } else if (imcValue < 35){
                        return "Obesidade Grau I";
                       } else if (imcValue < 40){
                        return "Obesidade Grau II";
                       } else {
                        return "Obesidade Grau III";
                       }
                case R.id.radioFem:
                    if (imcValue < 16) {
                        return "Magreza Grave";
                       } else if (imcValue < 18.5) {

                        return "Magreza Leve";
                       } else if (imcValue < 25) {

                        return "Saudável";
                       } else if (imcValue < 30) {

                        return "Sobrepeso";
                       } else if (imcValue < 35){
                        return "Obesidade Grau I";
                       } else if (imcValue < 40){
                        return "Obesidade Grau II";
                       } else {
                        return "Obesidade Grau III";
                       }
           }
        return null;
       }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
package com.example.calculadorimc;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.RadioGroup;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
私人放射组rgsexo;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calculateClickHandler(视图){
//确保我们处理计算器按钮的点击
if(view.getId()==R.id.botaCalcular){
//获取小部件的引用
EditText editPeso=(EditText)findViewById(R.id.editPeso);
EditText editAltura=(EditText)findViewById(R.id.editAltura);
TextView imcView=(TextView)findViewById(R.id.imcView);
//从小部件引用获取用户值
float-peso=float.parseFloat(editPeso.getText().toString());
float altura=float.parseFloat(editAltura.getText().toString());
//计算bmi值
浮动imc值=计算单位(比索,阿尔图拉);
//解释bmi值的含义
字符串IMC解释=解释IMC(IMC值);
//现在在结果文本中设置值
imcView.setText(imcValue+“-”+imc解释);
}
}
//计算体重指数的公式
//查证http://en.wikipedia.org/wiki/Body_mass_index
私人浮动利率(浮动比索、浮动阿尔图拉){
收益(浮动)(比索/(阿尔图拉*阿尔图拉));
}
//解读体重指数的含义
私有字符串解释IMC(浮点IMC值){
int selectedId=rgsexo.getCheckedRadioButtonId();//获取id
开关(selectedId)//打开所选按钮
{
案例R.id.radioMasc:
如果(imc值<16){
返回“马格里扎坟墓”;
}否则如果(IMC值<18.5){
返回“Magreza Leve”;
}否则如果(IMC值<25){
返回“Saudável”;
}否则如果(IMC值<30){
返回“sobrepso”;
}否则如果(IMC值<35){
返回“Obesidade Grau I”;
}否则如果(IMC值<40){
返回“Obesidade Grau II”;
}否则{
返回“Obesidade Grau III”;
}
案例R.id.radioFem:
如果(imc值<16){
返回“马格里扎坟墓”;
}否则如果(IMC值<18.5){
返回“Magreza Leve”;
}否则如果(IMC值<25){
返回“Saudável”;
}否则如果(IMC值<30){
返回“sobrepso”;
}否则如果(IMC值<35){
返回“Obesidade Grau I”;
}否则如果(IMC值<40){
返回“Obesidade Grau II”;
}否则{
返回“Obesidade Grau III”;
}
}
返回null;
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
这是我的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/rgSexo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radioMasc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/radioMasc" />

        <RadioButton
            android:id="@+id/radioFem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radioFem" />
    </RadioGroup>

    <TextView
        android:id="@+id/alturaView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/alturaView" />

    <EditText
        android:id="@+id/editAltura"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:inputType="numberDecimal"
        android:text="@string/editAltura" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/pesoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pesoView" />

    <EditText
        android:id="@+id/editPeso"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:inputType="number"
        android:text="@string/editPeso" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip"
        android:background="#111111" />

    <Button
        android:id="@+id/botaoCalcular"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="calculateClickHandler"
        android:text="@string/botaoCalcular" />

    <TextView
        android:id="@+id/imcView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/imcView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

在调用
getCheckedRadioButtonId
方法之前,也将
rgsexo
RadioGroup实例初始化为:

     rgsexo = (RadioGroup)findViewById(R.id.rgSexo);

您没有定义id…,只定义了radiogroup的id

  rgsexo = (RadioGroup)findViewById(R.id.rgSexo); 

把你的日志放在plzz….上,你得到了什么错误?对于初学者,在
解释imc
方法中使用的变量
imcValue
似乎没有定义