我正在开发简单的BMI计算器,因为我通过单选按钮添加了性别对我的代码的影响,AVD意外停止。问题出在InterpretIMC函数上,我相信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;
}
}
这是我的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>
答案 0 :(得分:5)
在调用rgsexo
方法之前初始化getCheckedRadioButtonId
RadioGroup实例:
rgsexo = (RadioGroup)findViewById(R.id.rgSexo);
答案 1 :(得分:2)
你没有定义id ..,只需定义radiogroup的id
rgsexo = (RadioGroup)findViewById(R.id.rgSexo);