0

I am working on my first app (so please forgive me if I've made a silly mistake, I am relatively new to coding). I created a settings activity for my app, but the moment I click on the settings button from the action bar, my app crashes. I tried debugging the app, turns out the moment I remove the lines where I keep the switches on setchecked, it works fine, but then if I remove the app from memory and open it again, the setting isnt saved, the switches aren't on. Please help.

package com.example.taskmasterv3;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Switch;

public class SettingsActivity extends AppCompatActivity {

    public static final String SETTINGS_PREFERENCES = "com.example.taskmasterv3.SettingsPreferences";
    Switch switchReminder, switchNotifications;
    boolean reminders;
    boolean notifications;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        SharedPreferences prefs = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE);
        reminders = prefs.getBoolean("reminders", false);
        notifications = prefs.getBoolean("notifications", false);

        switchReminder.setChecked(reminders);
        switchNotifications.setChecked(notifications);

        switchReminder = findViewById(R.id.switchReminder);
        switchNotifications = findViewById(R.id.switchNotifications);

        if (switchReminder.isChecked()) {
            reminders = true;
            SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
            editor.putBoolean("reminders", reminders);
            editor.commit();
        }

        if (switchNotifications.isChecked()) {
            notifications = true;
            SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
            editor.putBoolean("notifications", notifications);
            editor.commit();

        }
    }
}
Leo
  • 2,097
  • 21
  • 35
Aadhitya
  • 83
  • 11
  • 1
    Crashes are always accompanied by a stack trace, what is the that stack trace in your case? – luk2302 Jan 08 '21 at 13:24
  • Yeah do provide the stacktrace, but also take a look at the preference fragment for settings given in jetpack [here](https://developer.android.com/guide/topics/ui/settings) for easier handling. – CyberShark Jan 08 '21 at 13:29
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Jan 08 '21 at 13:30
  • This is the stack trace (sorry I didnt know this existed before your comment) java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmasterv3/com.example.taskmasterv3.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setChecked(boolean)' on a null object reference – Aadhitya Jan 08 '21 at 13:30

1 Answers1

1

You're not using findViewById to retrieve your switches in onCreate before you're using them, so they're null:

switchReminder.setChecked(reminders);
switchNotifications.setChecked(notifications); <-- wrong order



switchReminder = findViewById(R.id.switchReminder); <-- too late, already tried to use it above
switchNotifications = findViewById(R.id.switchNotifications);

it should be:

switchReminder = findViewById(R.id.switchReminder);
switchNotifications = findViewById(R.id.switchNotifications);

switchReminder.setChecked(reminders);
switchNotifications.setChecked(notifications);
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • It doesn't crash now. Thanks for that. But it still doesnt work as it is supposed to. If I remove the app from memory, it should show me the last settings I had saved. But it still shows me the default false values (ie the switches are off). Can you help with that too please? – Aadhitya Jan 08 '21 at 13:42
  • you'd have to post another question for that, unfortunately, i'm done for today :) i'm sure someone will be able to help you though – a_local_nobody Jan 08 '21 at 13:51