I'm doing a termometric scale converter in Android Studio, but when I input a number on any EditText and press the calculate button it crashes and I have no idea why.
This is the MainActivity.java
code:
private ViewHolder mViewHolder = new ViewHolder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mViewHolder.editValueCel = findViewById(R.id.edit_value_cel);
this.mViewHolder.editValueFahr = findViewById(R.id.edit_value_fahr);
this.mViewHolder.editValueKelvin = findViewById(R.id.edit_value_kelvin);
this.mViewHolder.buttonCalculate = findViewById(R.id.button_calculate);
this.mViewHolder.textCelsius = findViewById(R.id.tot_celcius);
this.mViewHolder.textFahr = findViewById(R.id.tot_fahr);
this.mViewHolder.textKelvin = findViewById(R.id.tot_kelvin);
this.mViewHolder.buttonCalculate.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button_calculate){
String valorCel = this.mViewHolder.editValueCel.getText().toString();
String valorFahr = this.mViewHolder.editValueFahr.getText().toString();
String valorKelvin = this.mViewHolder.editValueKelvin.getText().toString();
if (("".equals(valorCel)) && ("".equals(valorFahr)) && ("".equals(valorKelvin))){
Toast.makeText(this, this.getString(R.string.informe_valor), Toast.LENGTH_SHORT).show();
}else {
Double celsius = Double.valueOf(valorCel);
Double fahr = Double.valueOf(valorFahr);
Double kelvin = Double.valueOf(valorKelvin);
if (("".equals(valorFahr)) && ("".equals(valorKelvin))){
this.mViewHolder.textCelsius.setText(String.valueOf(celsius));
this.mViewHolder.textFahr.setText(String.valueOf((9*celsius+160)/5));
this.mViewHolder.textKelvin.setText(String.valueOf(celsius+273));
}else if(("".equals(valorCel)) && ("".equals(valorKelvin))){
this.mViewHolder.textCelsius.setText(String.valueOf(((fahr-32)/9)*5));
this.mViewHolder.textFahr.setText(String.valueOf(fahr));
this.mViewHolder.textKelvin.setText(String.valueOf((5*fahr+2297)/9));
}else if(("".equals(valorCel)) && ("".equals(valorFahr))){
this.mViewHolder.textCelsius.setText(String.valueOf(kelvin-273));
this.mViewHolder.textFahr.setText(String.valueOf((9*kelvin-2297)/5));
this.mViewHolder.textKelvin.setText(String.valueOf(kelvin));
}else{
Toast.makeText(this, this.getString(R.string.erro_muitos_valores), Toast.LENGTH_SHORT).show();
}
}
}
}
private static class ViewHolder{
EditText editValueCel;
EditText editValueFahr;
EditText editValueKelvin;
TextView textCelsius;
TextView textFahr;
TextView textKelvin;
Button buttonCalculate;
}
}
If there's need to post the activity_main.xml code here just tell me and I'll post it.
You convert strings to doubles first
Double celsius = Double.valueOf(valorCel);
Double fahr = Double.valueOf(valorFahr);
Double kelvin = Double.valueOf(valorKelvin);
but judging from the code that follows most likely one or more of the strings are empty which gives a NumberFormatException
(or null which gives a NullPointerException
) so you need to move the conversions to be inside one of the if/else conditions.
It is better to use isEmpty()
than comparing if a string equals ""
if (valorFahr.isEmpty() && valorKelvin.isEmpty()){
Double celsius = Double.valueOf(valorCel);
this.mViewHolder.textCelsius.setText(String.valueOf(celsius));
this.mViewHolder.textFahr.setText(String.valueOf((9*celsius+160)/5));
this.mViewHolder.textKelvin.setText(String.valueOf(celsius+273));
} else if(valorCel.isEmpty() && valorKelvin.isEmpty()){
Double fahr = Double.valueOf(valorFahr);
this.mViewHolder.textCelsius.setText(String.valueOf(((fahr-32)/9)*5));
this.mViewHolder.textFahr.setText(String.valueOf(fahr));
this.mViewHolder.textKelvin.setText(String.valueOf((5*fahr+2297)/9));
} else if(valorCel.isEmpty() && valorFahr.isEmpty()){
Double kelvin = Double.valueOf(valorKelvin);
this.mViewHolder.textCelsius.setText(String.valueOf(kelvin-273));
this.mViewHolder.textFahr.setText(String.valueOf((9*kelvin-2297)/5));
this.mViewHolder.textKelvin.setText(String.valueOf(kelvin));
...
Not knowing how the rest of your app works it might still be a good thing to check that not all 3 strings are empty or that any of them are null as part of this code.
So maybe before the if/else part do
if ((valorFahr == null || valorKelvin == null || valorCel == null) ||
(valorFahr.isEmpty() && valorKelvin.isEmpty() && valorCel.isEmpty())) {
//some error handling
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.