使用递归来解决用户输入的表达式(android calculator app)

时间:2015-07-09 01:37:50

标签: java android calculator

以下是我的计算器应用程序的MainActivity.java代码:

package com.example.android.calculatorrecursion;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {
    private String numString = "";
    private ArrayList<String> keyArrayList;
    EditText editTextView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextView = (EditText)findViewById(R.id.textView);
    }

    //when number buttons are clicked.
    public void onButtonClick(View v) {
        button = (Button) v;
        numString += button.getText().toString();
        editTextView.setText(numString);
    }

    //operation buttons which will also add a space before and after the      operator.
    public void onButtonSpaceGenerator(View view)
    {
        button = (Button)view;
        numString += " ";
        numString += button.getText().toString();
        numString += " ";
        editTextView.setText(numString);
    }

    public void onClear(View v){
        keyArrayList.clear();
        numString = "";
        editTextView.setText("");
    }

    public void createArray(){
        String tempString;
        int index;
        int begin = 0;
        while(begin < numString.length())
        {
          if(numString.contains(" ")) {
            index = numString.indexOf(" ", begin);
            tempString = numString.substring(begin, index);
            keyArrayList.add(tempString);
            begin += index + 1;
           }
        }
    }

    public void equals(View v){
        createArray();
        double total = calcResult(keyArrayList);
        editTextView.setText(Double.toString(total));
        numString = "";
    }

    public double calcResult(ArrayList<String> tempKeyArrayListArrayList){
         double tempNum Double.parseDouble(tempKeyArrayListArrayList.get(0));
         tempKeyArrayListArrayList.remove(0);
         if(tempKeyArrayListArrayList.size() >= 1) {
            String operator = tempKeyArrayListArrayList.get(0);
            tempKeyArrayListArrayList.remove(0);
            if (operator.equals("+"))
                return tempNum + calcResult(tempKeyArrayListArrayList);
            if (operator.equals("*"))
                return tempNum * calcResult(tempKeyArrayListArrayList);
            if (operator.equals("-"))
                return tempNum - calcResult(tempKeyArrayListArrayList);
            if (operator.equals("/"))
                return tempNum / calcResult(tempKeyArrayListArrayList);
         }
      return tempNum;
     }
}

但是,我目前有一个错误,说

java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3679)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.android.calculatorrecursion.MainActivity.createArray(MainActivity.java:57)
        at com.example.android.calculatorrecursion.MainActivity.equals(MainActivity.java:64)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java
       :1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)

我不确定为什么当我没有尝试初始化该行代码中的任何变量时,计算机会抛出第三个错误。 如果任何人能够解释/澄清代码的错误以及前两个错误的意思,我将非常感激。谢谢!

新错误:

    java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3679)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
        at java.util.ArrayList.get(ArrayList.java:304)
        at com.example.android.calculatorrecursion.MainActivity.calcResult(MainActivity.java:72)
        at com.example.android.calculatorrecursion.MainActivity.calcResult(MainActivity.java:82)
        at com.example.android.calculatorrecursion.MainActivity.equals(MainActivity.java:66)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3674)
        at android.view.View.performClick(View.java:4203)
        at android.view.View$PerformClick.run(View.java:17189)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4950)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您收到NullPointerException,因为您从未初始化keyArrayList

NPE发生在这一行:keyArrayList.add(tempString);

只需在onCreate()初始化它。

public class MainActivity extends ActionBarActivity {
    private String numString = "";
    private ArrayList<String> keyArrayList;
    EditText editTextView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextView = (EditText)findViewById(R.id.textView);

        keyArrayList = new ArrayList<String>(); //added
    }

建议阅读:What is a NullPointerException, and how do I fix it?