每按一次结果按钮,应用程序崩溃

时间:2018-06-17 18:06:34

标签: android calculator

E / AndroidRuntime:致命异常:主要                       过程:com.vineet.calculator,PID:29435                       java.lang.NullPointerException:尝试调用虚方法' boolean java.lang.Boolean。enter code here booleanValue()'在null对象引用上                           在com.vineet.calculator.MainActivity $ 16.onClick(MainActivity.java:177)                           在android.view.View.performClick(View.java:6294)                           在android.view.View $ PerformClick.run(View.java:24770)                           在android.os.Handler.handleCallback(Handler.java:790)                           在android.os.Handler.dispatchMessage(Handler.java:99)                           在android.os.Looper.loop(Looper.java:164)                           在android.app.ActivityThread.main(ActivityThread.java:6494)                           at java.lang.reflect.Method.invoke(Native Method)                           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)     申请已终止。

**Activity.java**

package com.vineet.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    Button btn0;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn7;
    Button btn8;
    Button btn9;
    Button btnResult;
    Button btnClear;
    Button btnExit;

    Button btnAdd;
    Button btnSub;
    Button btnMul;
    Button btnDiv;

    EditText etNumber;
    TextView tvAnswer;

    int val1, val2;
    Boolean boolMul, boolAdd, boolSub, boolDiv;



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

        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btnAdd = findViewById(R.id.btnAdd);
        btnSub = findViewById(R.id.btnSub);
        btnMul = findViewById(R.id.btnMul);
        btnDiv = findViewById(R.id.btnDiv);
        btnResult = findViewById(R.id.btnResult);
        btnClear = findViewById(R.id.btnClear);
        btnExit = findViewById(R.id.btnExit);
        etNumber = findViewById(R.id.etNumber);
        tvAnswer = findViewById(R.id.tvAnswer);


        btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"0");
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"1");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"2");
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"3");
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"4");
            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"5");
            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"6");
            }
        });
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"7");
            }
        });
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"8");
            }
        });
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText(etNumber.getText()+"9");
            }
        });
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etNumber.setText("");
                tvAnswer.setText("");
            }
        });


        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                val1 = Integer.valueOf(etNumber.getText().toString());
                boolAdd = true;
                etNumber.setText(null);
            }
        });

        btnSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                val1 = Integer.valueOf(etNumber.getText().toString());
                boolSub = true;
                etNumber.setText(null);
            }
        });

        btnMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                val1 = Integer.valueOf(etNumber.getText().toString());
                boolMul = true;
                etNumber.setText("");
            }
        });

        btnDiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                val1 = Integer.valueOf(etNumber.getText().toString());
                boolDiv = true;
                etNumber.setText("");
            }
        });

        btnResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                val2 = Integer.valueOf(etNumber.getText().toString());

                if(boolAdd == true){
                    tvAnswer.setText(val1 + val2+"");
                    boolAdd = false;
                }
                if(boolSub == true){
                    tvAnswer.setText(val1 - val2 + "");
                    boolSub = false;
                }

            }
        });

    }
}

2 个答案:

答案 0 :(得分:4)

您正在使用Boolean(非原始数据类型)而不是boolean

您必须初始化布尔值才能使用:

Boolean boolMul = new Boolean(false);

同样初始化所有布尔变量或使用布尔值。

答案 1 :(得分:1)

使用“boolean”而不是“Boolean”。 Boolean是一个类,它在对象中包装基本类型boolean的值。避免使用它,因为与原始类型相比,它需要更多的内存。

此外,许多setOnclickListener不是编写sooo,而是在MainActivity中实现View.OnClickListener,并使用Switch case来处理按钮点击。