Android Taschenrechner - crash

Darano

Mitglied
Hey Community,
Ich habe vor zwei Tagen angefangen, mich ein wenig mit Android Studio auseinander zu setzen und wollte jetzt mal eine kleine Taschenrechner app coden, die folgender maßen aussehen soll.


Dazu folgende XML Datei:
Code:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RGroup"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="133dp"
        android:layout_marginTop="122dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="133dp">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Addition"
            android:id="@+id/add"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subtraktion"
            android:id="@+id/sub"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Multiplikation"
            android:id="@+id/mul"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Division"
            android:id="@+id/div"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Quadr.Wurzel"
            android:id="@+id/wrzl"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Quadrat"
            android:id="@+id/quad"
            android:onClick="onRadioButtonClicked"/>

    </RadioGroup>

    <EditText
        android:id="@+id/zahl1"
        android:layout_width="165dp"
        android:layout_height="0dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text=""
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/zahl2"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/zahl2"
        android:layout_marginLeft="16dp" />

    <EditText
        android:id="@+id/zahl2"
        android:layout_width="0dp"
        android:layout_height="39dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text=""
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="35dp"
        android:layout_marginEnd="34dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="37dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/zahl1"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="34dp" />

    <Button
        android:id="@+id/btnCalc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calculate"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/erg"
        android:layout_marginStart="7dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="21dp"
        app:layout_constraintLeft_toLeftOf="@+id/RGroup"
        android:layout_marginLeft="7dp"
        android:onClick="calc"/>

    <EditText
        android:id="@+id/erg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Ergebnis"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="81dp"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="155dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="81dp" />

</android.support.constraint.ConstraintLayout>

Und folgende JAVA Datei:

Code:
package com.example.yannik.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View;
import android.widget.Toast;

import java.lang.*;

public class MainActivity extends AppCompatActivity {

    public int whatdoto = 0;

    final public Button calc = (Button)findViewById(R.id.btnCalc);

    final public EditText zahl11 = (EditText)findViewById(R.id.zahl1);
    final public EditText zahl22 = (EditText)findViewById(R.id.zahl2);
    final public EditText erg = (EditText)findViewById(R.id.erg);

    [MENTION=295804]Override[/MENTION]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void calc(View view){
        int z1 = Integer.parseInt(zahl11.getText().toString());
        int z2 = Integer.parseInt(zahl22.getText().toString());
        int ergebnis = 0;
        double dergebnis = 0.0;

        switch (whatdoto){
            case 1:
                ergebnis = z1 + z2;
                erg.setText(String.valueOf(ergebnis));
                break;
            case 2:
                ergebnis = z1 - z2;
                erg.setText(String.valueOf(ergebnis));
                break;
            case 3:
                ergebnis = z1 * z2;
                erg.setText(String.valueOf(ergebnis));
                break;
            case 4:
                ergebnis = z1 / z2;
                erg.setText(String.valueOf(ergebnis));
                break;
            case 5:
                ergebnis = (z1 + z2) * 2;
                erg.setText(String.valueOf(ergebnis));
                break;
            case 6:
                double a = Double.parseDouble(zahl11.getText().toString());
                double b = Double.parseDouble(zahl22.getText().toString());
                dergebnis = Math.sqrt(a+b);
                erg.setText(String.valueOf(dergebnis));
                break;
            case 0:
               // Toast.makeText(this, "Welches Verfahren soll angewandt werden?", Toast.LENGTH_LONG).show();
                break;
        }
    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.add:
                if (checked)
                    whatdoto = 1;
                    break;
            case R.id.sub:
                if (checked)
                    whatdoto = 2;
                    break;
            case R.id.mul:
                if (checked)
                    whatdoto = 3;
                    break;
            case R.id.div:
                if (checked)
                    whatdoto = 4;
                    break;
            case R.id.quad:
                if (checked)
                    whatdoto = 5;
                    break;
            case R.id.wrzl:
                if (checked)
                    whatdoto = 6;
                    break;
            default:
                whatdoto = 0;
break;
        }
    }
}

Ich kriege keinen Syntax Fehler angezeigt und die App lässt sich kompilieren und ausführen, jedoch crasht sie sofort.
Also muss es ja ein semantischer Fehler sein, oder?:confused:

Wäre froh, wenn ihr mir auf die Sprünge helfen könntet, und wenn euch noch irgendetwas anderes negatives auffällt (style, umständlich gecoded, ...) bin ich für alle Tipps dankbar.

Liebe Grüße,
Yannik
 

krgewb

Top Contributor
Ich weiß nicht ob es daran liegt aber du schreibst an einer Stelle:
ergebnis = z1 / z2;

Das führt zu falschen Werten weil alle drei Variablen vom Typ int sind.
 

Robat

Top Contributor
Informier dich mal was der LogCat in Android studio ist. Schau dir an was dort für Fehlermeldung kommen und poste den stacktrace hier.
 

Darano

Mitglied
Vielen lieben Dank für doe LogCat Methode! Sehr nützlich!
Habe dadurch den Fehler gefunden ( NullPointerException bei den globalen Variablenzuordnung :rolleyes: )
Und auch danke für den Hinweis auf den Divisionsfehler!

Liebe Grüße
 

Ähnliche Java Themen

Neue Themen


Oben