Android Studio Android计算器崩溃

rggaifut  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(208)

我正在做一个简单的Android计算器,必须支持+,-,*,/。
当我尝试按下任何按钮时-应用程序崩溃,经过一番调查,我发现问题出在内部的if语句中:

public void buttonFunctionNumber(View view) {
        if (view instanceof Button){
            Button button = (Button) view;
            String str = button.getText().toString();
            
            if (Double.parseDouble(result.getText().toString()) == 0)
                result.setText(str);
            else
                result.append(str);
        }
    }

if:在用户按下所需的数字后,在他按下第二个数字之前,计算器的屏幕将显示0。因此,在此if中,我想检查结果是否为0(result是计算器屏幕显示的内容),以了解用户是否想继续到下一个数字。
else:用户希望继续向该数添加更多位以进行计算,因此我们追加下一位。
我想知道我怎样才能解决这个问题。
感谢您抽出宝贵时间:)
整个代码:

package com.example.myfirstproj;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView result;
    double num1=0.0, num2=0.0;
    String sign = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result = findViewById(R.id.textViewResult);
    }

    public void buttonFunctionNumber(View view) {
        if (view instanceof Button){
            Button button = (Button) view;
            String str = button.getText().toString();

            if (Double.parseDouble(result.getText().toString()) == 0.0)
                result.setText(str);
            else
                result.append(str);
        }
    }

    public void buttonEqualFunction(View view) {
        if (view instanceof Button) {
            String temp2 = result.getText().toString();
            num2 = Double.parseDouble(temp2);
            double res = 0.0;

            switch (sign) {
                case "+":
                    res = num1 + num2;
                    break;
                case "-":
                    res = num1 - num2;
                    break;
                case "*":
                    res = num1 * num2;
                    break;
                case "/":
                    if (num2 != 0.0)
                        res = num1 / num2;
                    else
                        Toast.makeText(this, "Error Cant divide by 0", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;

            }
            num1 = res;
            result.setText(res + "");
        }
    }

    public void buttonAddFunction(View view) {
        if (view instanceof Button){
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "+";
            result.setText(" ");
        }
    }

    public void buttonSubtractFunction(View view) {
        if (view instanceof Button) {
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "-";
            result.setText("0");
        }
    }

    public void buttonMultiplyFunction(View view) {
        if (view instanceof Button) {
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "*";
            result.setText("0");
        }
    }

    public void buttonDivideFunction(View view) {
        if (view instanceof Button) {
            String temp1 = result.getText().toString();
            num1 = Double.parseDouble(temp1);
            sign = "/";
            result.setText("0");
        }

    }

    public void buttonClearFunction(View view) {
        result.setText("0");
    }
}
cwtwac6a

cwtwac6a1#

请尝试在解析之前修整结果并首先检查是否为null和空

String result = result.getText().toString()).trim();
if (result != null && result.length() > 0) {
   if (Double.parseDouble(result) == 0.0)
      result.setText(str);
   else
      result.append(str);
}

相关问题