注册页面不会使用Firebase记录用户数据

时间:2018-04-07 20:09:37

标签: android firebase nullpointerexception

public class RegisterActivity extends AppCompatActivity {

    //create variables
    private EditText mUsername, mPassword, mEmail, mPhone;
    private Button mRegister;
    private TextView mLogin;
    private FirebaseAuth mAuth;
    private ImageView mProfilePicture;
    String user_email, user_name, user_phone, user_password;

    //on creation of activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setupViews();

        //firebase instance declared
        mAuth = FirebaseAuth.getInstance();

        //When register is clicked
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //if all fields are okay using validation method
                if(validate()){
                    //get user info
                    String user_email = mEmail.getText().toString().trim();
                    String user_password = mPassword.getText().toString().trim();
                    String user_phone = mPhone.getText().toString().trim();
                    String user_name = mUsername.getText().toString().trim();

                    mAuth.createUserWithEmailAndPassword(user_email, user_password)
                            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful()) {
                                sendEmailVerification();
                                FirebaseUser user = mAuth.getCurrentUser();
                                Toast.makeText(RegisterActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                                finish();
                                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                            }else{
                                Toast.makeText(RegisterActivity.this, "Registration Failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
        //Return user back to login page
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
            }
        });
    }


    //setup views by id from xml
    private void setupViews(){
        mUsername = (EditText)findViewById(R.id.editTextNewUsername);
        mPassword = (EditText)findViewById(R.id.editTextNewPassword);
        mEmail = (EditText)findViewById(R.id.editTextNewEmail);
        mPhone = (EditText)findViewById(R.id.editTextPhone);
        mRegister = (Button)findViewById(R.id.buttonRegister);
        mLogin = (TextView)findViewById(R.id.textViewLogin);
        mProfilePicture = (ImageView)findViewById(R.id.imageViewProfilePicture);
    }

    //validate if all fields are okay and not empty
    private Boolean validate() {
        Boolean result = false;

        //convert inputs to strings
        String user_name = mUsername.getText().toString();
        String user_password = mPassword.getText().toString();
        String user_email = mEmail.getText().toString();
        String user_phone = mPhone.getText().toString();

        //check if any fields are empty, if not then return result
        if (user_name.isEmpty() || user_password.isEmpty() || user_email.isEmpty() || user_phone.isEmpty()) {
            Toast.makeText(this, "All fields are required for registration.", Toast.LENGTH_SHORT).show();
        } else {
            result = true;
        }
        return result;
    }
    private void sendEmailVerification(){
        FirebaseUser firebaseUser = mAuth.getCurrentUser();
        if(firebaseUser!=null){
            firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        sendUserData();
                        Toast.makeText(RegisterActivity.this, "Successfully Registered, Verification mail sent!", Toast.LENGTH_SHORT).show();
                        mAuth.signOut();
                        finish();
                        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                    }else{
                        Toast.makeText(RegisterActivity.this, "Verification mail has'nt been sent!", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void sendUserData(){
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference myRef = firebaseDatabase.getReference(mAuth.getCurrentUser().getUid());
        UserProfile userProfile = new UserProfile(user_phone, user_email, user_name);
        myRef.setValue(userProfile);
    }
}

解决了我的个人资料页面的崩溃,并得到它以保存人工数据,但发现我的注册页面是问题,而不是记录数据。这是我的Userprofile.java供参考

public class UserProfile {
    public String user_phone;
    public String user_email;
    public String user_name;

    public UserProfile() {
    }

    public UserProfile(String user_phone, String user_email, String user_name) {
        this.user_phone = user_phone;
        this.user_email = user_email;
        this.user_name = user_name;
    }

    public String getUser_phone() {
        return user_phone;
    }

    public void setUser_phone(String user_phone) {
        this.user_phone = user_phone;
    }

    public String getUser_email() {
        return user_email;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
}

任何帮助将不胜感激,我已经广泛阅读了该主题,似乎无法看到导致阻碍的任何小问题。任何其他指针也欢迎。

1 个答案:

答案 0 :(得分:0)

尝试更改:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
    assert userProfile != null;
    mProfileName.setText(userProfile.getUser_name());
    mProfileNumber.setText(userProfile.getUser_phone());
    mProfileEmail.setText(userProfile.getUser_email());
}

使用:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
    if(userProfile != null){
        mProfileName.setText(userProfile.getUser_name());
        mProfileNumber.setText(userProfile.getUser_phone());
        mProfileEmail.setText(userProfile.getUser_email());
    }
}