Reputation: 11
I have created a simple android app that take two number and display them by adding. It work properly when I enter values but if I don't enter values and hit the button the app crashes..guide me please and thanks in advance Here is its XML Code
<EditText
android:id="@+id/et_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="number" />
<EditText
android:id="@+id/et_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_first"
android:hint="Enter second number"
android:inputType="number" />
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="addition"
android:text="+" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_add"
android:textAppearance="?android:textAppearanceLarge" />
Here is its MainActivity.Java code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addition(View view) {
EditText first_number = (EditText) findViewById(R.id.et_first);
float first = Float.parseFloat(first_number.getText().toString());
EditText second_number = (EditText) findViewById(R.id.et_second);
float second = Float.parseFloat(second_number.getText().toString());
TextView tv_result = (TextView) findViewById(R.id.tv_result);
float result = first + second;
tv_result.setText(String.valueOf(result));
}
}
Upvotes: 0
Views: 52
Reputation: 11903
You need to check that the EditText
actually has text in it first or else you will get a NumberFormatException
when you try and parse an empty String into a float.
String firstNumber = first_number.getText().toString();
float first = TextUtils.isEmpty(firstNumber) ? 0 : Float.parseFloat(firstNumber);
If you also want to handle invalid inputs as mentioned by a comment try the following method:
private float parseFloat(String floatStr) {
if(TextUtils.isEmpty(floatStr)) return 0;
try {
return Float.parseFloat(floatStr);
} catch(NumberFormatException e) {
return 0;
}
}
You can replace 0 with any other default you would like to use.
You could then do the following:
float first = parseFloat(first_number.getText().toString());
Upvotes: 1
Reputation: 2493
Try this,
EditText first_number = (EditText) findViewById(R.id.et_first);
EditText second_number = (EditText) findViewById(R.id.et_second);
String first = first_number.getText().toString();
String second = second_number.getText().toString();
if(!first.isEmpty() && !second.isEmpty()){
float firstNumber = Float.parseFloat(first);
float secondNumber = Float.parseFloat(second);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
float result = firstNumber + secondNumber;
tv_result.setText(String.valueOf(result));
}
Upvotes: 0
Reputation: 2284
It is simple, when you are not entering any number and still pressing the add button it will crash coz it will perform addition on 'NULL' and app will crash.
public void addition(View view) {
EditText first_number = (EditText) findViewById(R.id.et_first);
if(!first_number.equals("") && first_number!=null)
float first = Float.parseFloat(first_number.getText().toString());
EditText second_number = (EditText) findViewById(R.id.et_second);
if(!second_number .equals("") && second_number !=null)
float second = Float.parseFloat(second_number.getText().toString());
TextView tv_result = (TextView) findViewById(R.id.tv_result);
float result = first + second;
tv_result.setText(String.valueOf(result));
}
In this way you will be checking for the null or empty EditText and hence your App won't crash, if you want more user friendly add Else block and throw some message via Toast :)
Happy Coding....
Upvotes: 0
Reputation: 4767
You need to write something like this:
float first = 0.0, second = 0.0;
EditText first_number = (EditText) findViewById(R.id.et_first);
if(first_number.getText() != null && first_number.getText().length() > 0){
first = Float.parseFloat(first_number.getText().toString());
}
if(second_number.getText() != null && second_number.getText().length() > 0){
first = Float.parseFloat(first_number.getText().toString());
}
EditText second_number = (EditText) findViewById(R.id.et_second);
float second = Float.parseFloat(second_number.getText().toString());
TextView tv_result = (TextView) findViewById(R.id.tv_result);
float result = first + second;
tv_result.setText(String.valueOf(result));
Upvotes: 0