I am using switch buttons in my Android app and I want to check the database for a value and depending on the value, the switch button should be checked on creation of the activity. The problem is that after clicking on the button, the activity ends and I cannot figure out why this happens. I don't get any errors when I run or debug the program, so I cannot follow any error trail. Any assistance on this issue would be greatly appreciated.
public class EditProfile extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    DatabaseReference root, profilePicRef;
    StorageReference storageReference;
    StorageTask uploadTask;;
    Switch EPprivacySwitchSW;
    User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);
        root = FirebaseDatabase.getInstance().getReference();
        user = (User) intent.getSerializableExtra("user_object");
        EPprivacySwitchSW = findViewById(R.id.EPprivacySW);
        if (user.getProfile_status() != null && user.getProfile_status().equals("private")){
            EPprivacySwitchSW.setChecked(true);
        }
        EPprivacySwitchSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    profileStatus = "private";
                }else{
                    profileStatus = "public";
                }
                root.child("users").child(user.getUsername())
                    .child("profile_status").setValue(profileStatus);
            }
        });
    }
}
 
     
    