I’m using Android Studio to create a Calculator App with java.
If I press the “CE” or the “=” button twice it the app closed by itself.
I still can’t understand why.
I tought that it could have something to do with the zahl1 and zahl2 value, but i don’t know why and how.
public class MainActivity extends AppCompatActivity
{
// TextViews
private TextView tvBerechnung;
private TextView tvResultat;
// Zahlen
private Button num0;
private Button num1;
private Button num2;
private Button num3;
private Button num4;
private Button num5;
private Button num6;
private Button num7;
private Button num8;
private Button num9;
// Funktionen
private Button back;
private Button remove;
private Button clear;
private Button gleich;
private Button punkt;
// Operatoren
private Button plus;
private Button minus;
private Button mal;
private Button durch;
private String zahl1 = "";
private String zahl2 = "";
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
// TextViews
tvBerechnung = findViewById(R.id.tvBerechnung);
tvResultat = findViewById(R.id.tvResultat);
// Zahlen
num0 = findViewById(R.id.btn0);
num1 = findViewById(R.id.btn1);
num2 = findViewById(R.id.btn2);
num3 = findViewById(R.id.btn3);
num4 = findViewById(R.id.btn4);
num5 = findViewById(R.id.btn5);
num6 = findViewById(R.id.btn6);
num7 = findViewById(R.id.btn7);
num8 = findViewById(R.id.btn8);
num9 = findViewById(R.id.btn9);
// Funktionen
back = findViewById(R.id.btnBack);
remove = findViewById(R.id.btnC);
clear = findViewById(R.id.btnCE);
gleich = findViewById(R.id.btnGleich);
punkt = findViewById(R.id.btnPunkt);
// Operatoren
plus = findViewById(R.id.btnPlus);
minus = findViewById(R.id.btnMinus);
mal = findViewById(R.id.btnMal);
durch = findViewById(R.id.btnDurch);
// Click listener für die num buttons
onNumClick(num0, "0");
onNumClick(num1, "1");
onNumClick(num2, "2");
onNumClick(num3, "3");
onNumClick(num4, "4");
onNumClick(num5, "5");
onNumClick(num6, "6");
onNumClick(num7, "7");
onNumClick(num8, "8");
onNumClick(num9, "9");
// Click listener für die Operatoren
onOperatorClick(plus, "+");
onOperatorClick(minus, "-");
onOperatorClick(mal, "*");
onOperatorClick(durch, "/");
// tvBerechnung und tvResultat leeren
tvBerechnung.setText("");
tvResultat.setText("");
}
// Zahlen in tvBerechnung anzeigen
public void onNumClick(Button button, String value)
{
button.setOnClickListener(v ->
{
// falls ein operator in tvBerechnung steht, denn tvBerechnung leeren und die Zahl hinzufügen
if (!Objects.equals(operator, ""))
{
tvBerechnung.setText("");
tvBerechnung.append(value);
zahl2 = tvBerechnung.getText().toString();
}
else
{
tvBerechnung.append(value);
zahl1 = tvBerechnung.getText().toString();
}
});
}
// wenn auf C geklickt wird tvBerechnung und tvResultat leeren
public void onClearClick(View view)
{
tvBerechnung.setText("");
tvResultat.setText("");
zahl1 = "";
zahl2 = "";
operator = "";
}
// wenn auf CE geklickt wird eine Zahl von tvBerechnung löschen
public void onRemoveClick(View view)
{
// holt String aus tvBerechnung
String str = tvBerechnung.getText().toString();
if (str == null)
{
return;
}
else
{
// löscht das letzte Zeichen, 0 ist das erste index, also length -> total index - 1
str = str.substring(0, (str.length()) - 1);
// neuen String
tvBerechnung.setText(str);
}
}
// damit nicht mehr als ein Punkt in tvBerechnung steht
public void onPunktClick(View view)
{
String str = tvBerechnung.getText().toString();
if (str.contains("."))
{
return;
}
else
{
tvBerechnung.append(".");
}
}
// für den +/- Button, also entwerde + oder -
public void onPlusMinusClick(View view)
{
String str = tvBerechnung.getText().toString();
// von - zu + wechseln
if (str.contains("-"))
{
// löscht das erste Zeichen
str = str.substring(1);
tvBerechnung.setText("");
tvBerechnung.setText(str);
}
// zu - wechseln
else
{
tvBerechnung.setText("-" + str);
}
// falls operator leer ist, dann ist es die erste Zahl
if (operator.isEmpty())
{
zahl1 = tvBerechnung.getText().toString();
}
else
{
zahl2 = tvBerechnung.getText().toString();
}
}
// für die Operatoren, also tvBerechnung leeren und den Operator hinzufügen
public void onOperatorClick(Button button, String value)
{
button.setOnClickListener(v ->
{
tvBerechnung.setText("");
tvBerechnung.append(value);
operator = value;
});
}
// Berechnung
public void onGleichClick(View view)
{
float z1 = Float.parseFloat(zahl1);
float z2 = Float.parseFloat(zahl2);
switch (operator)
{
// Addition
case "+":
float summe = z1 + z2;
tvResultat.setText(String.valueOf(summe));
break;
// Subtraktion
case "-":
float differenz = z1 - z2;
tvResultat.setText(String.valueOf(differenz));
break;
// Multiplikation
case "*":
float produkt = z1 * z2;
tvResultat.setText(String.valueOf(produkt));
break;
// Division
case "/":
float quotient = z1 / z2;
tvResultat.setText(String.valueOf(quotient));
break;
}
}
}
I’ve tried debuggin, but it takes too long to load, and i also tried going trough the code again line by line but still couldn’t figure out the cause.
dedushka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.