Here is the MainActivity.java code of my calculator app:
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;
}
}
But, I currently have an error that says,
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)
And i'm not sure why the computer throws the third error when i'm not trying to initialize any variable in that line of code. If anybody would be able to explain/clarify what is wrong with the code and what those first two errors mean, I would really appreciate it. Thanks!
The new error:
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)