Reputation: 25
Why when my Edit Text in XML are empty and when I click on the button envoyerListener
my application crashes?
You could find below the Java code, which is maybe not that good, but the application runs well. Only when I click on the button envoyer
with my two EditText
s being empty my application crashes.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private final String defaut = "result";
EditText price = null;
EditText rent = null;
TextView result = null;
Button buttenvoyer = null;
Button buttrent = null;
Button buttprice = null;
Button buttinfo = null;
Button buttclear = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
price = (EditText) findViewById(R.id.price);
rent = (EditText) findViewById(R.id.rent);
result = (TextView) findViewById(R.id.result);
buttenvoyer = (Button) findViewById(R.id.buttcalculate);
buttrent = (Button) findViewById(R.id.buttrent);
buttprice = (Button) findViewById(R.id.buttprice);
buttinfo = (Button) findViewById(R.id.buttinfo);
buttclear = (Button) findViewById(R.id.buttclear);
price.addTextChangedListener(textWatcher);
rent.addTextChangedListener(textWatcher);
buttenvoyer.setOnClickListener(envoyerListener);
buttrent.setOnClickListener(rentListener);
buttprice.setOnClickListener(priceListener);
buttinfo.setOnClickListener(infoListener);
buttclear.setOnClickListener(clearListener);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int befor, int count) {
result.setText(defaut);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
private OnClickListener envoyerListener = new OnClickListener() {
@Override
public void onClick(View v) {
String p = price.getText().toString();
String r = rent.getText().toString();
float pValue = Float.valueOf(p);
float rValue = Float.valueOf(r);
float resultat = rValue * 100 / pValue;
result.setText("the profit is " + String.valueOf(resultat) + " %");
}
};
private OnClickListener rentListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent jumpage = new Intent(MainActivity.this, Rent.class);
startActivity(jumpage);
}
};
private OnClickListener priceListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent jumpage = new Intent(MainActivity.this, Price.class);
startActivity(jumpage);
}
};
private OnClickListener infoListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent jumpage = new Intent(MainActivity.this, Infoam.class);
startActivity(jumpage);
}
};
private OnClickListener clearListener = new OnClickListener() {
@Override
public void onClick(View v) {
price.getText().clear();
rent.getText().clear();
result.setText(defaut);
}
};
}
Upvotes: 0
Views: 69
Reputation: 306
You say:
only when I click on the button envoyer with my two Edit text empty my application crash
So the issue is here:
String p = price.getText().toString();
String r = rent.getText().toString();
float pValue = Float.valueOf(p);
float rValue = Float.valueOf(r);
In your listener you don't check these strings. If they are empty, which happens when you grab empty text, then you have no values to parse. And as a result it throws NumberFormatException.
You have to perform some check when you get the text p
and r
because you'll just have an empty string ""
which cannot be cast into anything and choose to either set the value of the String
to 0 or choose to set your float
value to 0.
I would do something like:
float pValue;
if(p.isEmpty())
{
pValue = 0;
}
else
{
pvalue = Float.valueOf(p);
}
Upvotes: 3