单击加/减/除/乘按钮两次时,计算器应用程序崩溃

时间:2019-02-11 10:37:21

标签: java android crash calculator

我正在制作一个简单的计算器。一切正常。但是问题是当我两次单击加/减/除/乘按钮时,应用程序崩溃了。

我从logcat中发现了问题'java.lang.StringToReal.parseDouble(StringToReal.java:248)'

我添加了logcat但无法解决。

我的代码:

public class MainActivity extends AppCompatActivity {
Button one,two, three, four, five, six, seven, eight, nine, plus, minus, 
multiple, divide, clear, equal;
TextView textView1;
TextView result;
double numOne,numTwo, total;
boolean mAdd, mMinus, mMul, mDiv;


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



    one= (Button) findViewById(R.id.one);
    two= (Button) findViewById(R.id.two);
    ........
    plus= (Button) findViewById(R.id.plus);
    minus= (Button) findViewById(R.id.minus);
    divide= (Button) findViewById(R.id.divide);
    multiple= (Button) findViewById(R.id.multiple);
    clear= (Button) findViewById(R.id.clear);
    equal= (Button) findViewById(R.id.equal);
    result=(TextView)findViewById(R.id.result);
    textView1=(TextView) findViewById(R.id.textView1);

    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView1.setText(textView1.getText() + "1");
        }
    });

    two.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView1.setText(textView1.getText() + "2");
        }
    });

    .........

    plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            numOne=Double.parseDouble(textView1.getText() + "");
            mAdd=true;
            textView1.setText(null);
        }
    });
    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            numOne=Double.parseDouble(textView1.getText() + "");
            mMinus=true;
            textView1.setText(null);
        }
    });
    divide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            numOne=Double.parseDouble(textView1.getText() + "");
            mDiv=true;
            textView1.setText(null);
        }
    });
    multiple.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            numOne=Double.parseDouble(textView1.getText() + "");
            mMul=true;
            textView1.setText(null);
        }
    });


    equal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           numTwo=Double.parseDouble(textView1.getText() + "");
           if(mAdd==true){
                total=numOne+numTwo;
                textView1.setText(total + "");

               mAdd=false;
           }

            if(mMinus==true){
                total=numOne-numTwo;
                textView1.setText(total+"");
                mMinus=false;
            }

            if(mMul==true){
                total=numOne*numTwo;
                textView1.setText(total+"");
                mMul=false;
            }

            if(mDiv==true){
                total=numOne/numTwo;
                textView1.setText(total+"");
                mDiv=false;
            }
      }});
}}

Logcat:

02-11 05:11:54.958 2274-2274/com.example.android.calculator 
E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
MAX_TEXTURE_SIZE: 16384
02-11 05:15:27.626 2274-2274/com.example.android.calculator 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.calculator, PID: 2274
java.lang.NumberFormatException: Invalid double: ""
    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
    at java.lang.StringToReal.parseDouble(StringToReal.java:248)
    at java.lang.Double.parseDouble(Double.java:295)
    at

com.example.android.calculator.MainActivity$10.onClick(MainActivity.java:102)

3 个答案:

答案 0 :(得分:1)

是的,它会产生NumberFormatException的错误,因为当您从numOne=Double.parseDouble(textView1.getText() + "");这样的文本视图中获取文本时,由于“”

尝试这样,它将正常工作

if(!TextUtils.isEmpty(textView1.getText())){

     numOne=Double.parseDouble(textView1.getText());

}

答案 1 :(得分:0)

textView1.setText(textView1.getText()+“ 1”);该行可以写为

textView1.setText(textView1.getText()。toString()+“ 1”);

要将您的值解析为字符串格式,请尝试此操作!

答案 2 :(得分:0)

每次单击加/减/除/按钮后,您的textview文本都设置为“”,并且无法将“”转换为双精度,必须先检查该值,然后才能将其转换为双精度 >

if(!TextUtils.isEmpty(textView1.getText())){
 //write your logic here

}