I am currently using AndroidHive\'s tutorial to learn how to use Firebase, I am starting to understand the methods and documentation well now - I realised that Firebase offe
Undo the addition of .getDatabase(); first.
This is not a native Firebase method, my mistake.
I got your problem.
Make FirebaseDatabase a global variable and initialise it in onCreate. It requires a context at the point.
Just take FirebaseDatabase database; before onCreate.
And In, onCreate: initialise, database=FirebaseDatabase.getInstance();.
Don't initialise in method. I'll edit my previous answer soon.
Try this:
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
generateUser(email, password)
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});
This is the method that is being called above.
public void generateUser(String username, String password)
{
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference users = database.getReference("users"); //users is a node in your Firebase Database.
User user = new User(username, password); //ObjectClass for Users
users.push().setValue(user);
}
Also: User.class
public class User {
String username;
String password;
public User() {
//Empty Constructor For Firebase
}
public User(String username, String password)
{
this.username = username; //Parameterized for Program-Inhouse objects.
this.password = password;
}
//Getters and Setters
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}