点击没有数字的运算符后Android计算器应用程序崩

时间:2014-07-29 09:26:58

标签: performance android-layout android-intent android-activity android-fragments

我在Android上创建了一个计算器,但是如果我没有输入数字并且我点击了一个操作,我的应用程序将崩溃并说“#34; Unfortunetly你的应用程序名称已停止"”,你能帮助解决这个问题吗?抓住那个例外?,......

    btn1 = (Button) findViewById(R.id.btn1Id);
    btn2 = (Button) findViewById(R.id.btn2Id);
    btn3 = (Button) findViewById(R.id.btn3Id);
    btn4 = (Button) findViewById(R.id.btn4Id);
    btn5 = (Button) findViewById(R.id.btn5Id);
    btn6 = (Button) findViewById(R.id.btn6Id);
    btn7 = (Button) findViewById(R.id.btn7Id);
    btn8 = (Button) findViewById(R.id.btn8Id);
    btn9 = (Button) findViewById(R.id.btn9Id);
    btn0 = (Button) findViewById(R.id.btn0Id);

    btnPlus = (Button) findViewById(R.id.btnPlusId);
    btnMinus = (Button) findViewById(R.id.btnMinusId);
    btnTimes = (Button) findViewById(R.id.btnTimesId);
    btnDivide = (Button) findViewById(R.id.btnDivideId);
    btnModulo = (Button) findViewById(R.id.btnModuloId);
    btnEquals = (Button) findViewById(R.id.btnEqualsId);
    btnCA = (Button) findViewById(R.id.btnCAId);

    MainDisplay = (TextView) findViewById(R.id.MainDisplayId);

    try{

        btn1.setOnClickListener((OnClickListener) this);
        btn2.setOnClickListener((OnClickListener) this);
        btn3.setOnClickListener((OnClickListener) this);
        btn4.setOnClickListener((OnClickListener) this);
        btn5.setOnClickListener((OnClickListener) this);
        btn6.setOnClickListener((OnClickListener) this);
        btn7.setOnClickListener((OnClickListener) this);
        btn8.setOnClickListener((OnClickListener) this);
        btn9.setOnClickListener((OnClickListener) this);
        btn0.setOnClickListener((OnClickListener) this);
        btnPlus.setOnClickListener((OnClickListener) this);
        btnMinus.setOnClickListener((OnClickListener) this);
        btnTimes.setOnClickListener((OnClickListener) this);
        btnDivide.setOnClickListener((OnClickListener) this);
        btnModulo.setOnClickListener((OnClickListener) this);
        btnEquals.setOnClickListener((OnClickListener) this);

    }catch(Exception e){

    }

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"1";
            MainDisplay.setText(getNum);

        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"2";
            MainDisplay.setText(getNum);

        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"3";
            MainDisplay.setText(getNum);

        }
    });
    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"4";
            MainDisplay.setText(getNum);

        }
    });
    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"5";
            MainDisplay.setText(getNum);

        }
    });
    btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"6";
            MainDisplay.setText(getNum);

        }
    });
    btn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"7";
            MainDisplay.setText(getNum);

        }
    });
    btn8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"8";
            MainDisplay.setText(getNum);

        }
    });
    btn9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"9";
            MainDisplay.setText(getNum);

        }
    });
    btn0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"0";
            MainDisplay.setText(getNum);

        }
    });
    btnPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "+";
            MainDisplay.setText("");
        }
    });
    btnMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "-";
            MainDisplay.setText("");
        }
    });
    btnTimes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "*";
            MainDisplay.setText("");
        }
    });
    btnDivide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "/";
            MainDisplay.setText("");
        }
    });
    btnModulo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "%";
            MainDisplay.setText("");
        }
    });
    btnEquals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num2 = Integer.parseInt(MainDisplay.getText().toString());
            if(op=="+"){
            ans = num1 + num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="-"){
            ans = num1 - num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="*"){
            ans = num1 * num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="/"){
            ans = num1 / num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="%"){
            ans = num1 % num2;
            MainDisplay.setText(Integer.toString(ans));
            }
        }
    });

    btnCA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            MainDisplay.setText("");
        }
    });
}
enter code hereprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button) findViewById(R.id.btn1Id);
    btn2 = (Button) findViewById(R.id.btn2Id);
    btn3 = (Button) findViewById(R.id.btn3Id);
    btn4 = (Button) findViewById(R.id.btn4Id);
    btn5 = (Button) findViewById(R.id.btn5Id);
    btn6 = (Button) findViewById(R.id.btn6Id);
    btn7 = (Button) findViewById(R.id.btn7Id);
    btn8 = (Button) findViewById(R.id.btn8Id);
    btn9 = (Button) findViewById(R.id.btn9Id);
    btn0 = (Button) findViewById(R.id.btn0Id);

    btnPlus = (Button) findViewById(R.id.btnPlusId);
    btnMinus = (Button) findViewById(R.id.btnMinusId);
    btnTimes = (Button) findViewById(R.id.btnTimesId);
    btnDivide = (Button) findViewById(R.id.btnDivideId);
    btnModulo = (Button) findViewById(R.id.btnModuloId);
    btnEquals = (Button) findViewById(R.id.btnEqualsId);
    btnCA = (Button) findViewById(R.id.btnCAId);

    MainDisplay = (TextView) findViewById(R.id.MainDisplayId);

    try{

        btn1.setOnClickListener((OnClickListener) this);
        btn2.setOnClickListener((OnClickListener) this);
        btn3.setOnClickListener((OnClickListener) this);
        btn4.setOnClickListener((OnClickListener) this);
        btn5.setOnClickListener((OnClickListener) this);
        btn6.setOnClickListener((OnClickListener) this);
        btn7.setOnClickListener((OnClickListener) this);
        btn8.setOnClickListener((OnClickListener) this);
        btn9.setOnClickListener((OnClickListener) this);
        btn0.setOnClickListener((OnClickListener) this);
        btnPlus.setOnClickListener((OnClickListener) this);
        btnMinus.setOnClickListener((OnClickListener) this);
        btnTimes.setOnClickListener((OnClickListener) this);
        btnDivide.setOnClickListener((OnClickListener) this);
        btnModulo.setOnClickListener((OnClickListener) this);
        btnEquals.setOnClickListener((OnClickListener) this);

    }catch(Exception e){

    }

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"1";
            MainDisplay.setText(getNum);

        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"2";
            MainDisplay.setText(getNum);

        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"3";
            MainDisplay.setText(getNum);

        }
    });
    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"4";
            MainDisplay.setText(getNum);

        }
    });
    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"5";
            MainDisplay.setText(getNum);

        }
    });
    btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"6";
            MainDisplay.setText(getNum);

        }
    });
    btn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"7";
            MainDisplay.setText(getNum);

        }
    });
    btn8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"8";
            MainDisplay.setText(getNum);

        }
    });
    btn9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"9";
            MainDisplay.setText(getNum);

        }
    });
    btn0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            getNum = MainDisplay.getText().toString();
            getNum = getNum+"0";
            MainDisplay.setText(getNum);

        }
    });
    btnPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "+";
            MainDisplay.setText("");
        }
    });
    btnMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "-";
            MainDisplay.setText("");
        }
    });
    btnTimes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "*";
            MainDisplay.setText("");
        }
    });
    btnDivide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "/";
            MainDisplay.setText("");
        }
    });
    btnModulo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num1 = Integer.parseInt(MainDisplay.getText().toString());
            op = "%";
            MainDisplay.setText("");
        }
    });
    btnEquals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            num2 = Integer.parseInt(MainDisplay.getText().toString());
            if(op=="+"){
            ans = num1 + num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="-"){
            ans = num1 - num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="*"){
            ans = num1 * num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="/"){
            ans = num1 / num2;
            MainDisplay.setText(Integer.toString(ans));
            }else if(op=="%"){
            ans = num1 % num2;
            MainDisplay.setText(Integer.toString(ans));
            }
        }
    });

    btnCA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            MainDisplay.setText("");
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我无法看到你在哪里声明操作,但我认为它是一个字符串?

尝试

if(op.equals("")){
//Do nothing
}

你的代码中也有很多重复,并且可以编写一些方法来摆脱重复,而不是使用数百个监听器只使用一个和一些if语句