Android Taschenrechner

Endymion

Bekanntes Mitglied
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:

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);
	}
}
[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:eek: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:eek:rientation="horizontal" >

<Button
android:id="@+id/delete_all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek: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:eek:nClick="deleteLast"
android:text="@string/delete_last" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek:rientation="horizontal" >

<Button
android:id="@+id/seven"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek: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:eek: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:eek: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:eek:nClick="clickOperand"
android:text="@string/divide" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek:rientation="horizontal" >

<Button
android:id="@+id/four"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek: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:eek: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:eek: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:eek:nClick="clickOperand"
android:text="@string/multiplicate" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek:rientation="horizontal" >

<Button
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek: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:eek: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:eek: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:eek:nClick="clickOperand"
android:text="@string/substract" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek:rientation="horizontal" >

<Button
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:eek: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:eek: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:eek: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:eek: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]
 
Zuletzt bearbeitet:

schlingel

Gesperrter Benutzer
Die Exception wäre noch ganz nützlich um herauszufinden welche Zeile genau welches Objekt betrifft.

:autsch: - wofür hast du die Zahlen in den Resourcen? Hast du Angst, dass man übermorgen statt 4 etwas anderes schreiben könnte?
 

Endymion

Bekanntes Mitglied
In diesem LogCat von Eclipse steht keine Zeilenangabe und die Konsole kann ich bei Android-Apps irgendwie nicht verwenden, Systemausgaben werden mir jedenfalls nicht angezeigt. Gibt es eine Möglichkeit, die Zeile herauszufinden?
Die alten Römer dachten auch nicht, dass man in Rom eines Tages arabische Zahlen verwenden würde.
 

Endymion

Bekanntes Mitglied
Der Stacktrace ist aber etwas anders als in der normalen Konsole. Ich poste mal das, was sich markieren lässt:

02-05 16:52:23.860: E/AndroidRuntime(2049): FATAL EXCEPTION: main
02-05 16:52:23.860: E/AndroidRuntime(2049): java.lang.IllegalStateException: Could not execute method of the activity
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.view.View$1.onClick(View.java:2144)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.view.View.performClick(View.java:2485)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.view.View$PerformClick.run(View.java:9080)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.os.Handler.handleCallback(Handler.java:587)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.os.Looper.loop(Looper.java:123)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-05 16:52:23.860: E/AndroidRuntime(2049): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:52:23.860: E/AndroidRuntime(2049): at java.lang.reflect.Method.invoke(Method.java:507)
02-05 16:52:23.860: E/AndroidRuntime(2049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-05 16:52:23.860: E/AndroidRuntime(2049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-05 16:52:23.860: E/AndroidRuntime(2049): at dalvik.system.NativeStart.main(Native Method)
02-05 16:52:23.860: E/AndroidRuntime(2049): Caused by: java.lang.reflect.InvocationTargetException
02-05 16:52:23.860: E/AndroidRuntime(2049): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:52:23.860: E/AndroidRuntime(2049): at java.lang.reflect.Method.invoke(Method.java:507)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.view.View$1.onClick(View.java:2139)
02-05 16:52:23.860: E/AndroidRuntime(2049): ... 11 more
02-05 16:52:23.860: E/AndroidRuntime(2049): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.content.res.Resources.getText(Resources.java:201)
02-05 16:52:23.860: E/AndroidRuntime(2049): at android.widget.TextView.setText(TextView.java:2857)
02-05 16:52:23.860: E/AndroidRuntime(2049): at app.android.CalculatorActivity.calculate(CalculatorActivity.java:80)
02-05 16:52:23.860: E/AndroidRuntime(2049): ... 14 more
02-05 16:52:27.499: I/Process(2049): Sending signal. PID: 2049 SIG: 9
02-05 16:56:28.709: W/ResourceType(2116): No package identifier when getting value for resource number 0x00000002
02-05 16:56:28.709: D/AndroidRuntime(2116): Shutting down VM
02-05 16:56:28.719: W/dalvikvm(2116): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-05 16:56:28.729: E/AndroidRuntime(2116): FATAL EXCEPTION: main
02-05 16:56:28.729: E/AndroidRuntime(2116): java.lang.IllegalStateException: Could not execute method of the activity
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.view.View$1.onClick(View.java:2144)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.view.View.performClick(View.java:2485)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.view.View$PerformClick.run(View.java:9080)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.os.Handler.handleCallback(Handler.java:587)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.os.Looper.loop(Looper.java:123)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-05 16:56:28.729: E/AndroidRuntime(2116): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:56:28.729: E/AndroidRuntime(2116): at java.lang.reflect.Method.invoke(Method.java:507)
02-05 16:56:28.729: E/AndroidRuntime(2116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-05 16:56:28.729: E/AndroidRuntime(2116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-05 16:56:28.729: E/AndroidRuntime(2116): at dalvik.system.NativeStart.main(Native Method)
02-05 16:56:28.729: E/AndroidRuntime(2116): Caused by: java.lang.reflect.InvocationTargetException
02-05 16:56:28.729: E/AndroidRuntime(2116): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:56:28.729: E/AndroidRuntime(2116): at java.lang.reflect.Method.invoke(Method.java:507)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.view.View$1.onClick(View.java:2139)
02-05 16:56:28.729: E/AndroidRuntime(2116): ... 11 more
02-05 16:56:28.729: E/AndroidRuntime(2116): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.content.res.Resources.getText(Resources.java:201)
02-05 16:56:28.729: E/AndroidRuntime(2116): at android.widget.TextView.setText(TextView.java:2857)
02-05 16:56:28.729: E/AndroidRuntime(2116): at app.android.CalculatorActivity.calculate(CalculatorActivity.java:80)
02-05 16:56:28.729: E/AndroidRuntime(2116): ... 14 more
02-05 17:01:28.790: I/Process(2116): Sending signal. PID: 2116 SIG: 9
02-05 17:09:42.799: W/ResourceType(2128): No package identifier when getting value for resource number 0x00000002
02-05 17:09:42.809: D/AndroidRuntime(2128): Shutting down VM
02-05 17:09:42.809: W/dalvikvm(2128): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-05 17:09:42.820: E/AndroidRuntime(2128): FATAL EXCEPTION: main
02-05 17:09:42.820: E/AndroidRuntime(2128): java.lang.IllegalStateException: Could not execute method of the activity
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.view.View$1.onClick(View.java:2144)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.view.View.performClick(View.java:2485)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.view.View$PerformClick.run(View.java:9080)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.os.Handler.handleCallback(Handler.java:587)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.os.Looper.loop(Looper.java:123)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-05 17:09:42.820: E/AndroidRuntime(2128): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 17:09:42.820: E/AndroidRuntime(2128): at java.lang.reflect.Method.invoke(Method.java:507)
02-05 17:09:42.820: E/AndroidRuntime(2128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-05 17:09:42.820: E/AndroidRuntime(2128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-05 17:09:42.820: E/AndroidRuntime(2128): at dalvik.system.NativeStart.main(Native Method)
02-05 17:09:42.820: E/AndroidRuntime(2128): Caused by: java.lang.reflect.InvocationTargetException
02-05 17:09:42.820: E/AndroidRuntime(2128): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 17:09:42.820: E/AndroidRuntime(2128): at java.lang.reflect.Method.invoke(Method.java:507)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.view.View$1.onClick(View.java:2139)
02-05 17:09:42.820: E/AndroidRuntime(2128): ... 11 more
02-05 17:09:42.820: E/AndroidRuntime(2128): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.content.res.Resources.getText(Resources.java:201)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.widget.TextView.setText(TextView.java:2857)
02-05 17:09:42.820: E/AndroidRuntime(2128): at app.android.CalculatorActivity.calculate(CalculatorActivity.java:80)
02-05 17:09:42.820: E/AndroidRuntime(2128): ... 14 more



Ich finde hier aber keine Zeilenangabe..
 
Zuletzt bearbeitet:

eRaaaa

Top Contributor
Zeile 80 ist der Übeltäter:
textView.setText(result);

result ist ein int, er sucht jetzt also nach der resource mit der id "result" ....
->
textView.setText(String.valueOf(result));
 

eRaaaa

Top Contributor
Ok, wo steht, dass Zeile 80 schuld ist?

02-05 17:09:42.820: E/AndroidRuntime(2128): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.content.res.Resources.getText(Resources.java:201)
02-05 17:09:42.820: E/AndroidRuntime(2128): at android.widget.TextView.setText(TextView.java:2857)
02-05 17:09:42.820: E/AndroidRuntime(2128): at app.android.CalculatorActivity.calculate(CalculatorActivity.java:80)
 

Endymion

Bekanntes Mitglied
Ach so, ich dachte, der Ursprung wird immer oben aufgelistet. Ok, Zeile ist gefixt und ich habe das ganze mal auch für Fließkommazahlen umgeschrieben. Das ganze sieht jetzt folgendermaßen aus:
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<Double> numbers = new ArrayList<Double>();
		Matcher matcher = Pattern.compile("[\\d\\.]*").matcher(text);
		while (matcher.find()) {
			String s = matcher.group();
			if (!s.isEmpty()) {
				numbers.add(Double.parseDouble(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());
		double result = numbers.get(0);
		numbers.remove(0);
		while (numbers.size() > 0) {
			double 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 + "");
	}
}

Wenn ich jetzt 1.0+1.0 rechne, kommt 2.0 raus. Bei 1.2+1.3 kommt aber 1.2 raus und bei 1.3+1.2 kommt 1.3 raus. Es wird also nur die erste Zahl richtig verarbeitet. Was mache ich dabei falsch?
 

eRaaaa

Top Contributor
Debugge doch selbst mal ein bisschen. Dein Operator parsen mit dem Regex stimmt nicht, daher trifft dann in deinem switch-case auch kein Fall zu, d.h. es wird nichts gerechnet!
 

Ähnliche Java Themen

Neue Themen


Oben