I'm new to Java and Android development so I decided to make a simple app that takes in two fields and puts them in a simple math equation. However I have to convert the EditText into Int before I can do any math with them, right? In the XML file I have set both EditTexts to android:inputType="number". My attempts have been like this:
final EditText height = (EditText) findViewById (R.id.height); String heightString = height.getText().toString(); final int heightInt = Integer.parseInt(heightString);
and
final EditText height = (EditText) findViewById (R.id.height); final int heightInt = Integer.parseInt(height.getText().toString());
I also tried Integer.valueOf in both cases but it always force closes my app when I run it and if I comment out that int heightInt it doesn't force close.
Here's the whole code:
package com.body.calculator;
import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Vibrator; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class bmi extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView results = (TextView) findViewById(R.id.results); final EditText height = (EditText) findViewById (R.id.height); String heightString = height.getText().toString(); final int heightInt = Integer.parseInt(heightString); final EditText weight = (EditText) findViewById (R.id.weight); final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); Button submit_bmi = (Button) findViewById(R.id.submit_bmi); submit_bmi.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (height.getText().toString().equals("") && weight.getText().toString().equals("")) { results.setText("Both fields are empty"); vib.vibrate(100); } else if (height.getText().toString().equals("")) { results.setText("Height is empty"); vib.vibrate(100); } else if (weight.getText().toString().equals("")) { results.setText("Weight is empty"); vib.vibrate(100); } else { results.setText(""); } } }); } }
Any help would be appreciated. Thanks, Arnar.
You approach of
final EditText height = (EditText) findViewById (R.id.height);
String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
is correct, but you are calling this directly in your onCreate
method. This means they get called as soon as the app starts, even before the user is able to do anything. because of this the values are always empty, simply because the user hasn't even had the change to do anything yet.
When you use the values later in your onClick
(which is correct), they are still empty due to their early initialization.
Try putting this part:
String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
Directly after the start of your onClick
method but before your if/else
statements like so:
public void onClick(View v) {
String heightString = height.getText().toString();
final int heightInt = Integer.parseInt(heightString);
// Your statements
}