I want to add the number written in "Zahl 1" and add it with the number in EditText (in this example it is 1500). Then i want to rewrite the number in EditText to the new number. I thought the process was pretty simple and obvious but when i run the program and hit the add Button, the app crashes.
You can ignore the EditText "zahl2" for now. It is not important to my problem.
So here is the code:
package com.example.rechnen_speichern;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button save, addieren;
EditText zahl1, zahl2;
TextView ergebnis;
String finalergebnis;
SharedPreferences sp;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
save = findViewById(R.id.btnsave);
addieren = findViewById(R.id.btnadd);
zahl1 = findViewById(R.id.eingabe1);
zahl2 = findViewById(R.id.eingabe2);
ergebnis = findViewById(R.id.ergebnis);
sp = getSharedPreferences("myergebnis", Context.MODE_PRIVATE);
editor = sp.edit();
String data = sp.getString("key", "");
ergebnis.setText(data);
addieren.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int z1 = Integer.parseInt(zahl1.getText().toString());
int z4 = Integer.parseInt(zahl2.getText().toString());
int z2 = Integer.parseInt(ergebnis.getText().toString());
int z3 = z1 + z2; //add zahl1 + ergebnis -> then overwrite ergebnis with the new number z3
ergebnis.setText(String.valueOf(z3));
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finalergebnis = ergebnis.getText().toString();
editor.putString("key", finalergebnis);
editor.apply();
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
});
}
}
Here the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ergebnis"
android:textSize="50sp"
android:layout_gravity="center"
/>
<EditText
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/eingabe1"
android:hint="Zahl 1"
/>
<EditText
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/eingabe2"
android:hint="Zahl 2"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/btnadd"
android:text="Addieren"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:id="@+id/btnsave"
android:text="Save"/>
</LinearLayout>
So i tried the obvious. I set up 3 Integers: z1(the number you add), z2 (the given number), z3 (the sum of z1+z2). After that i set the EditText to z3. View of App
So in this case z1 (500) + z2 (1500) = z3 (2000) After i press the add button the EditText should display 2000.
The issue causing the app to crash likely lies in the part of your code where you attempt to parse the value of ergebnis (the TextView that displays the result) to an integer. When the app first runs, ergebnis might not contain a valid integer, which would cause a NumberFormatException when you attempt to parse it. Let's modify the code to handle this potential issue.
Here's an updated version of your MainActivity class with additional checks to prevent crashes:
Java:
package com.example.rechnen_speichern;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button save, addieren;
EditText zahl1, zahl2;
TextView ergebnis;
String finalergebnis;
SharedPreferences sp;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
save = findViewById(R.id.btnsave);
addieren = findViewById(R.id.btnadd);
zahl1 = findViewById(R.id.eingabe1);
zahl2 = findViewById(R.id.eingabe2);
ergebnis = findViewById(R.id.ergebnis);
sp = getSharedPreferences("myergebnis", Context.MODE_PRIVATE);
editor = sp.edit();
String data = sp.getString("key", "0"); // default to "0" if no value is stored
ergebnis.setText(data);
addieren.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int z1 = Integer.parseInt(zahl1.getText().toString());
int z2 = Integer.parseInt(ergebnis.getText().toString()); // This should now default to 0 initially
int z3 = z1 + z2; // add zahl1 + ergebnis -> then overwrite ergebnis with the new number z3
ergebnis.setText(String.valueOf(z3));
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Please enter valid numbers.", Toast.LENGTH_SHORT).show();
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finalergebnis = ergebnis.getText().toString();
editor.putString("key", finalergebnis);
editor.apply();
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
});
}
}
I hope I have been helpful!!