0

I want to create a chat app project with android studio and I am new developer when I want to save data in the firebase database my app crashed and this is my code:

public void register(final String username, final String email, final String password)
{
    auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                    if (task.isSuccessful())
                    {
                        FirebaseUser firebaseUser = auth.getCurrentUser();
                        assert firebaseUser != null;
                        String userid = firebaseUser.getUid();

                        HashMap<String , String> hashMap = new HashMap<>();
                        hashMap.put("id", userid);
                        hashMap.put("username", username);
                        hashMap.put("imageURL", "defualt");
                        hashMap.put("password", password);

                        reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

                        reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task)
                            {
                                if (task.isSuccessful())
                                {
                                    Intent intent = new Intent(RegisterActivity.this,
                                            MainActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
                    }
                    else
                        Toast.makeText(RegisterActivity.this, "You can't register woth this email or password",
                                Toast.LENGTH_SHORT).show();
                }
            });
}

and this task equal to false when I want to save my data:

                                    if (task.isSuccessful())
                                {
                                    Intent intent = new Intent(RegisterActivity.this,
                                            MainActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                    finish();
                                }
Abbas Jafari
  • 1,492
  • 2
  • 16
  • 28

1 Answers1

0

Make sure you are using Real Time Database instead of Cloud Firestore. That might be the case.

Abdu4
  • 1,269
  • 2
  • 11
  • 19