Hallo, ich versuche grade, die Taschenrechner-App für Android nachzuprogrammieren. Wenn ich 1+1 rechne und mir die numbers- und operatorliste ausgeben lasse, bekomme ich [1,1] und [+]. Beim berechnen bekomme ich aber eine IllegalStateException. Folgender Code:
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android
rientation="vertical" >
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
rientation="horizontal" >
<Button
android:id="@+id/delete_all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="deleteAll"
android:text="@string/delete_all" />
<Button
android:id="@+id/delete_last"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android
nClick="deleteLast"
android:text="@string/delete_last" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
rientation="horizontal" >
<Button
android:id="@+id/seven"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/seven" />
<Button
android:id="@+id/eight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/eight" />
<Button
android:id="@+id/nine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/nine" />
<Button
android:id="@+id/divide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/divide" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
rientation="horizontal" >
<Button
android:id="@+id/four"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/four" />
<Button
android:id="@+id/five"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/five" />
<Button
android:id="@+id/six"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/six" />
<Button
android:id="@+id/multiplicate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/multiplicate" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
rientation="horizontal" >
<Button
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/one" />
<Button
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/two" />
<Button
android:id="@+id/three"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/three" />
<Button
android:id="@+id/substract"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/substract" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
rientation="horizontal" >
<Button
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/point" />
<Button
android:id="@+id/zero"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/zero" />
<Button
android:id="@+id/is"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="calculate"
android:text="@string/is" />
<Button
android:id="@+id/plus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
nClick="clickOperand"
android:text="@string/plus" />
</LinearLayout>
</LinearLayout>[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Taschenrechner</string>
<string name="delete_all">delete</string>
<string name="delete_last">back</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="zero">0</string>
<string name="point">.</string>
<string name="divide">/</string>
<string name="multiplicate">x</string>
<string name="substract">-</string>
<string name="plus">+</string>
<string name="is">=</string>
</resources>[/XML]
Java:
package app.android;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CalculatorActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text_view);
}
public void clickOperand(View view) {
Button button = (Button) view;
String text = (String) button.getText();
textView.setText(textView.getText() + text);
}
public void deleteAll(View view) {
textView.setText("");
}
public void deleteLast(View view) {
String text = (String) textView.getText();
text = text.substring(0, text.length() - 1);
textView.setText(text);
}
public void calculate(View view) {
String text = (String) textView.getText();
ArrayList<Integer> numbers = new ArrayList<Integer>();
Matcher matcher = Pattern.compile("\\d*").matcher(text);
while (matcher.find()) {
String s = matcher.group();
if (!s.isEmpty()) {
numbers.add(Integer.parseInt(s));
}
}
ArrayList<Character> operators = new ArrayList<Character>();
matcher = Pattern.compile("\\D").matcher(text);
while (matcher.find()) {
String s = matcher.group();
if (!s.isEmpty()) {
operators.add(s.toCharArray()[0]);
}
}
textView.setText(numbers.toString() + operators.toString());
int result = numbers.get(0); // Bis exklusive hier fehlerfreier Verlauf
numbers.remove(0);
while (numbers.size() > 0) {
int operand = numbers.get(0);
switch (operators.get(0)) {
case '/':
result /= operand;
break;
case 'x':
result *= operand;
break;
case '-':
result -= operand;
break;
case '+':
result += operand;
break;
}
numbers.remove(0);
operators.remove(0);
}
textView.setText(result);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
<Button
android:id="@+id/delete_all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/delete_all" />
<Button
android:id="@+id/delete_last"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android
android:text="@string/delete_last" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
<Button
android:id="@+id/seven"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/seven" />
<Button
android:id="@+id/eight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/eight" />
<Button
android:id="@+id/nine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/nine" />
<Button
android:id="@+id/divide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/divide" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
<Button
android:id="@+id/four"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/four" />
<Button
android:id="@+id/five"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/five" />
<Button
android:id="@+id/six"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/six" />
<Button
android:id="@+id/multiplicate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/multiplicate" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
<Button
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/one" />
<Button
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/two" />
<Button
android:id="@+id/three"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/three" />
<Button
android:id="@+id/substract"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/substract" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
<Button
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/point" />
<Button
android:id="@+id/zero"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/zero" />
<Button
android:id="@+id/is"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/is" />
<Button
android:id="@+id/plus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android
android:text="@string/plus" />
</LinearLayout>
</LinearLayout>[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Taschenrechner</string>
<string name="delete_all">delete</string>
<string name="delete_last">back</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="zero">0</string>
<string name="point">.</string>
<string name="divide">/</string>
<string name="multiplicate">x</string>
<string name="substract">-</string>
<string name="plus">+</string>
<string name="is">=</string>
</resources>[/XML]
Zuletzt bearbeitet: