Firebase身份验证和数据库

时间:2017-02-20 17:31:17

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

我想使用firebase身份验证并获取Uid并将其与firebase数据库中有关用户的其他信息一起存储。

我有SignUpActivity和LoginActivity,我正在使用firebase身份验证(email,pwd)来注册和登录我的应用程序,这很好,但是当我添加一些关于用户的更多信息时,比如firstName,lastName,phoneNumber ,我在authUser上有一个错误的“NullpointerException”

SignUpActivity,ButtonSignUp:

New Module

这是logcat和Debugger:

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试在createUser方法中为Firebase实例使用不同的名称,我建议在方法中声明一个局部变量。

其次,如果用户注册成功则下面的if语句将始终评估为'false',并且当注册因任何原因失败时为true,因此请删除'!':

 if (!task.isSuccessful()) 
 {
  createUser(nom,prenom,numTel);
  Toast.makeText(SignupActivity.this, "Authentication failed." +  
   task.getException(),
   Toast.LENGTH_SHORT).show();

}

发布您的错误日志,可以帮助您更好地理解错误。希望这可以帮助。

-------下面的代码片段,根据需要进行修改.-------

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterActivity extends AppCompatActivity {

    private FirebaseAuth fbAuth;
    private EditText userNameField;
    private EditText passwordField;
    private EditText emailField;
    private Button registrationButton;
    private ProgressDialog progressDialog;
    private DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //initialize all variables/objects
        fbAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Users"); 
        userNameField = (EditText)findViewById(R.id.nameField);
        passwordField = (EditText)findViewById(R.id.passwordField);
        emailField = (EditText)findViewById(R.id.emailField);


        registrationButton = (Button)findViewById(R.id.registerButton);

        registrationButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
            startUserRegistration();
            }
        });

    }

    private void startUserRegistration() {
         //initialize all variables/objects
        FirebaseAuth fbAuth = FirebaseAuth.getInstance();
        final String name = userNameField.getText().toString().trim();
        String email = emailField.getText().toString().trim();
        String password = passwordField.getText().toString().trim();

        if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password))
        {



            fbAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {


                    if(task.isSuccessful())
                    {
                      //your logic is below, initialize the user object  
                    user = authUser.getCurrentUser();   

        if (user != null) {


        String userid = fbAuth.getCurrentUser().getUid();
        userInfos = new User(nom, prenom, numTel);
        myRef.child(id).setValue(userInfos);

            }

                    }
                }
            });
        }
    }
}