Sir George Sir George - 11 days ago 7
Java Question

Why does My app stops after i press the button

this is how i have registered the users..but it is not sending anything to database though the registration completes just fine

private void registerUser(){



//getting email and password from edit texts
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();

//checking if email and passwords are empty
if(TextUtils.isEmpty(email)){
Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
return;
}

if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
return;
}

//if the email and password are not empty
//displaying a progress dialog

progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();


//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}else{
//display some message here
Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});

}


then here is my login.activity. i can log in just fine but i wonder what cretiria is this code using to log in users??

private void userLogin(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();


//checking if email and passwords are empty
if(TextUtils.isEmpty(email)){
Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
return;
}

if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
return;
}

//if the email and password are not empty
//displaying a progress dialog

progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();

//logging in the user
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
//if the task is successfull
if(task.isSuccessful()){
//start the profile activity
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
}
});

}

Answer

change your person class like this

    public class Person {

    private String Username;
    private String Email;

    public Person(String email,String userName) {
     this.Username=userName;
     this.Email=email;

      /*Blank default constructor essential for Firebase*/
    }
    //Getters and setters
    public String getUsername() {
        return Username;
    }

    public void setUsername(String Username) {
        this.Username = Username;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }
}

then use it like this

 Person person=new Person();
 person.setUsername(Username);
 person.setEmail(Email);
 Firebase newRef = ref.child("Users").push();
 newRef.setValue(person);

then use this to get values

 newRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
               Person person = dataSnapshot.getValue(Person.class);
 String string = "Username: "+person.getName()+"\nEmail: "+person.getEmail()+"\n\n";

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
Comments