在Firebase数据库中存储用户信息

时间:2017-07-20 14:59:25

标签: android firebase firebase-realtime-database firebase-authentication

我尝试将用户ID,用户名和photoUrl信息存储到Firebase数据库中。我有用户私有字段,getter / setter等用户POJO类。这是我的UserDao类:

public class UserDao {

    private static final String USERS_TAG = "users";

    private FirebaseDatabase db = FirebaseDatabase.getInstance();
    private Map<String, User> users;
    private User currentUser;

   ....

    public void write(User user){

        db.getReference(USERS_TAG + "/" + user.getId()).setValue(user);
    }

    public boolean userExists(String userId){
        return users != null && users.containsKey(userId);
    }

    ....

}

这是RegisterEmailPasswordActivity类:

public class RegisterEmailPasswordActivity extends BaseActivity {

    ....


    public void registerUserWithEmailPassword(View view) {


        if(view == buttonRegister){
            String email = editTextEmail.getText().toString();
            String password = editTextPassword.getText().toString();
            if(TextUtils.isEmpty(email)){
                Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(password)){
                Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
                return;
            }

            progressDialog.setMessage("Registering user... ");
            progressDialog.show();

            firebaseAuth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful()){
                                storeToDb();
                                Toast.makeText(RegisterEmailPasswordActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();

                                progressDialog.dismiss();
                                startActivity(new Intent(RegisterEmailPasswordActivity.this, MainActivity.class));
                            }else{
                                Toast.makeText(RegisterEmailPasswordActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();

                                progressDialog.dismiss();

                            }
                        }
                    });

        }
    }

    private void storeToDb(){
        final FirebaseUser firebaseUser = FirebaseAuth
                .getInstance().getCurrentUser();
        if (userDao.userExists(firebaseUser.getUid())) {
            userDao.setCurrentUser(userDao.getUserById(firebaseUser.getUid()));
        } else {
            final User user = new User(firebaseUser.getUid(),
                    firebaseUser.getDisplayName(),
                    firebaseUser.getPhotoUrl().toString());
            userDao.write(user);
            userDao.setCurrentUser(user);
        }
    }
}

我得到了这个例外:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
                                                                                                   at com.example.android.materialdesigncodelab.activity.RegisterEmailPasswordActivity.storeToDb(RegisterEmailPasswordActivity.java:107)
                                                                                                   at com.example.android.materialdesigncodelab.activity.RegisterEmailPasswordActivity.access$000(RegisterEmailPasswordActivity.java:25)
                                                                                                   at com.example.android.materialdesigncodelab.activity.RegisterEmailPasswordActivity$1.onComplete(RegisterEmailPasswordActivity.java:82)
                                                                                                   at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
                                                                                                   at android.os.Handler.handleCallback(Handler.java:815)
                                                                                                   at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                                   at android.os.Looper.loop(Looper.java:207)
                                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

那么,我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

你必须自己做Auth吗?

否则您可以使用Firebase UI Auth

您可以让用户通过Google帐户,电子邮件,Facebook,Twitter进行身份验证。这取决于你需要什么。

希望这有帮助。

答案 1 :(得分:0)

我找到了这个问题。那是firebaseUser.getPhotoUrl().toString()。当我使用电子邮件/密码注册时,没有用户照片,因此在toString()对象上调用null导致NullPointerException