简体   繁体   English

由于某种原因,这崩溃了,我不知道为什么

[英]For some reason this is crashing and I have no idea why

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. 我正在Android Studio中进行温度换算,但是当我在任何EditText上输入数字并按下计算按钮时,它会崩溃,我也不知道为什么。

This is the MainActivity.java code: 这是MainActivity.java代码:


    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. 如果需要在此处发布activity_main.xml代码,请告诉我,我将其发布。

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. 但是从后面的代码判断,一个或多个字符串很可能为空,这会导致NumberFormatException (或者为null会导致NullPointerException ),因此您需要将转换移至if / else条件之一内。

It is better to use isEmpty() than comparing if a string equals "" 使用isEmpty()比比较字符串是否等于“”

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. 不知道应用程序其余部分的工作方式,作为此代码的一部分,检查并非所有3个字符串都为空或它们中的任何一个为null仍然是一件好事。

So maybe before the if/else part do 所以也许在if / else部分之前

if ((valorFahr == null || valorKelvin == null || valorCel == null) ||
    (valorFahr.isEmpty() && valorKelvin.isEmpty() && valorCel.isEmpty())) {
    //some error handling
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaFX应用程序冻结,我不知道为什么 - JavaFX Application Freezes, and I have no idea why 该行提供了一个空指针,我不知道为什么 - This line is giving a nullpointer and I have no idea why 出于某种原因,当我将其作为方法或通常在 if 语句中使用时,我的加载“圆圈”将不起作用。 有谁知道为什么? - For some reason, my loading "circle" will not working when I have it as a method or just normally in an if statement. Anyone know why? 我有一个由于某种原因没有被捕获的ConnectException - I have a ConnectException that isn't being caught for some reason 当我有 2 个 setOnclickListener 时,为什么我的应用程序不断崩溃? - Why does my app keep crashing when I have 2 setOnclickListener? 我不断收到ArrayIndexOutOfBoundsException,我不知道为什么 - I keep getting ArrayIndexOutOfBoundsException and I have no idea why 当我打开intelij想法时,Java正在崩溃 - Java is Crashing When i open intelij idea 由于某种随机原因,我的应用程序在 android 工作室中不断崩溃 - my app keeps crashing in android studio for some random reason 程序不会显示图片,我也不知道为什么 - Program won't display pictures and I have no idea why 我的PrintList函数的ArrayIndexOutOfBoundsException我不知道为什么 - ArrayIndexOutOfBoundsException for my PrintList function and I have no idea why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM