Android计算器EmptyStackException

时间:2018-04-14 22:34:19

标签: java android user-interface exception stack

我想制作一个堆栈计算器应用程序。但当我输入3 4 +时,我的应用程序崩溃并抛出EmptyStackException。

public class Calculator { // RechenLogik

 private Stack<Double> stack = new Stack<Double>();
 private MainActivity activity ;

public Calculator(MainActivity activity ) {
    this.activity = activity;
}


public void calculate() {
    String expr =activity.getTextfromTextField();

        switch (expr) {
            case "+":
                stack.push(stack.pop() + stack.pop());
                activity.setTextField(stack.peek().toString()); // set Text
                break;
            default:
                stack.push(Double.parseDouble(expr));
                break;
               }
           }
        }
     And in Main Activity in OnClick Method 
 case R.id.button17: r.calculate(); TextField.setText(""); break; // Enter 
  press

任何帮助都将不胜感激。

0 个答案:

没有答案