-1

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);
            }
        });
    }
}
DoubleU
  • 95
  • 3
  • 10
  • 1
    Follow [This](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) and provide the Stacktrace of Exception .. Seems like and NPE on `root` .. Because i do not see you initialized the `root`. – ADM Jan 08 '20 at 04:16
  • @ADM sorry about that. I did initialize the root and the data is retrieved from the database and data is written to the database when the switch is checked, but it also ends the activity with no error in the logcat, so I can't read the stack trace – DoubleU Jan 08 '20 at 05:16

2 Answers2

0

As per the @ADM 's comment.

You forgot to initialise the root variable in code.

Declare variable root as below.

private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
0

You need to instance the Firebase Realtime Database which is the DatabaseReference. So you should do something like this.

root = FirebaseDatabase.getInstance().getReference(); //Add here
root.child("users").child(user.getUsername()).child("profile_status").setValue(profileStatus);
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • I've localized the problem to this line in the listener ` root.child("users").child(user.getUsername()) .child("profile_status").setValue(profileStatus);` I cannot figure out why writing to the database causes the activity to stop. Any assistance with this issue would be greatly appreciated. Sidenote, the data is successfully written to the database before the activity ends – DoubleU Jan 08 '20 at 14:43
  • @DoubleU you have to check at logcat to see more details why the acitivity stopped. – Ticherhaz FreePalestine Jan 08 '20 at 14:48
  • @DoubleU the value stored only the apps is crashed? – Ticherhaz FreePalestine Jan 09 '20 at 04:03
  • yes, the value is stored, but the activity ends and goes back to the previous activity – DoubleU Jan 09 '20 at 05:17
  • @DoubleU after it crashed, can you check at tab 'Run'? we can see whats the error – Ticherhaz FreePalestine Jan 09 '20 at 06:29