I’m new to coding and a little iffy with my knowledge so forgive me if I’ve overlooked something simple, but I’ve been searching for 2 hours for an answer.
One of my classes is going to allow a user to input their height and weight, and return the BMI (Body Mass Index) for them. The problem is once they click on the button that performs the calculation and displays it in a TextView, nothing happens. The only error I see from LogCat is getCursorCapsMode on inactive InputConnection. This error only shows when I use the next button on the softkeyboard to move between EditText fields, so I don’t believe this is related, as there is still no result when I manually move between fields and don’t get this error. Feel free to assist with this issue too though, if you have any info.
This is duplicate code from a BMI android developing tutorial, word for word except for the id’s of the items, but the tutorial works perfectly when loaded into the emulator. My code is as follows:
public class FuncConverter extends DashMenuActivity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView (R.layout.dash_converter7);
Spinner converter_spinner = (Spinner) findViewById(R.id.converter_spinner);
ArrayAdapter<CharSequence> converter_adapter = ArrayAdapter.createFromResource(
this, R.array.converter_array, android.R.layout.simple_spinner_item);
converter_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
converter_spinner.setAdapter(converter_adapter);
}
public void calcClick (View view) {
if (view.getId() == R.id.convert_btn_calc) {
EditText weight_edit = (EditText) findViewById(R.id.weight_edit);
EditText height_edit = (EditText) findViewById(R.id.height_edit);
TextView bmi_text = (TextView) findViewById(R.id.bmi_text);
float weight = Float.parseFloat(weight_edit.getText().toString());
float height = Float.parseFloat(height_edit.getText().toString());
float bmiValue = calculateBMI (weight, height);
String bmiCalc = interpretBMI (bmiValue);
bmi_text.setText(bmiValue + " - " + bmiCalc);
}
}
private float calculateBMI (float weight, float height) {
return (float) (weight / (height * height));
}
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severley Underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Healthy weight";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}}
Its really hard for me to answer definitively because I am not sure how you are triggering the click to calculate the stuff. But from what i can tell
calcClick(View)is defined asonClick=calcClickin your xml. If this is true then you are getting the information the wrong way.What I did there was just move the
EditTextandTextViewas globals so then you have access to it incalcClick(View). If I am right , the reason it wasn’t working is because you don’t have access to theEditTextandTextViewviews from the xml outside ofonCreate(), at least in your case. What thatViewargument is that is getting passed is actually the button or whatever is triggering the event. Hopefully that makes sense. SoView viewisn’t the document root but rather(Button)viewor again, whatever you are clicking to trigger this.