I'm developing simple BMI calculator and since i added the influence of gender on my code with radio buttons the AVD stops unexpectedly. The problem is on the InterpretIMC function, i believe the ID of the radiobutton is not reading, don't know why! Please, a little help.
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;
}
}
Here is my xml file
<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>