问题
The app crash when there is no text ! I don't have what 'if' to put to fix that ! Thanks you
enter TextView TotalTextView;
EditText EnterPercentage;
EditText EnterPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TotalTextView = (TextView) findViewById(R.id.TotalTextView);
EnterPercentage = (EditText) findViewById(R.id.EnterPercentage);
EnterPrice = (EditText) findViewById(R.id.EnterPrice);
Button CalcBtn = (Button) findViewById(R.id.CalcBtn);
CalcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float percentage = Float.parseFloat(EnterPercentage.getText().toString());
float prix = Float.parseFloat(EnterPrice.getText().toString());
float dec = percentage / 100;
float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
TotalTextView.setText(String.format("%.2f", total));
}
});
Thank for your answer, I am a begineer so... Thanks !
if (EnterPercentage.getText().equals("")) {
float percentage = Float.parseFloat(EnterPercentage.getText().toString());
float prix = Float.parseFloat(EnterPrice.getText().toString());
float dec = percentage / 100;
float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
TotalTextView.setText(String.format("%.2f", total));
} else {
}
the button don't do anything but the app doesn't crash
回答1:
We check the length of the String... when it's 0 then we do nothing. When the String > 0 your code is running.
Like:
if (EnterPercentage.getText().trim().toString().length() == 0 || EnterPrice.getText().toString().length() == 0 ) {
//Textfields are empty.
Log.d("Error","Fields are empty");
} else {
//Textfield is full
float percentage = Float.parseFloat(EnterPercentage.getText().toString());
float prix = Float.parseFloat(EnterPrice.getText().toString());
float dec = percentage / 100;
float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
TotalTextView.setText(String.format("%.2f", total));
}
回答2:
You need to check (obviously) your string. The if that your code is missing is :
if (TextUtils.isEmpty(EnterPercentage.getText()) || TextUtils.isEmpty(EnterPrice.getText())) {
// YOUR CODE
}
Good luck with android development. Questions was already posted
回答3:
You surely got a NullPointerException. That's because the text is null and you put a .toString() after.
Try this:
float percenteage = 0;
float prix = 0;
if (EnterPercentage.getText() != null)
percenteage = Float.parseFloat(EnterPercentage.getText().toString());
if (EnterPrice.getText() != null)
prix = Float.parseFloat(EnterPercentage.getText().toString());
回答4:
you need to validate before trying to convert to float
float percentage = getFloat();
private float getFloat(){
float fRet = 0.0f;
if(your validation here){
fRet = Float.parseFloat(EnterPercentage.getText().toString());;
}
return fRet;
}
来源:https://stackoverflow.com/questions/40185478/how-to-stop-the-app-crashing-when-string-is-empty