My problem is that I cannot convert EditText to int. I've tried all the options I've read here, my app crashes on Open. Thank you in advance, I attach a part of my code where the problem is. My problem starts at the first parsing-trial with 'int kg'. I have no idea what to do. The declaration is there, but not seen here. Thank you in advance.
private void BMISzamitas(){
BMISzamolBtn=(Button)findViewById(R.id.BMISzamoloBtn);
kgEditText = (EditText)(findViewById(R.id.kgEditText));
cmEditText = (EditText)(findViewById(R.id.cmEditText));
eredmenyTextView=(TextView)(findViewById(R.id.bmiEredmenyTextView));
int kg = Integer.parseInt(kgEditText.getText().toString());
int cm = Integer.parseInt(cmEditText.toString());
int m = (cm*100);
double BMI = (double)(kg/Math.pow(m, m));
eredmenyTextView.setText(new Double(BMI).toString());
}
First set inputType="number"
to your EditText
which the user force to enter only number into the text editor to prevent exeption :
<EditText
android:id="@+id/kgEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" // this line
/>
then :
if (!kgEditText.getText().toString().isEmpty() &&
!cmEditText.getText().toString().isEmpty()) {
int kg = Integer.parseInt(kgEditText.getText().toString());
int cm = Integer.parseInt(cmEditText.getText().toString());
}