为什么getid从frabase身份验证得到错误?

时间:2017-09-03 07:19:05

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

目前我已成功将名称,电子邮件,密码和年龄插入firebase数据库,但在我尝试从选定的radiobutton中插入值之后,噩梦就来到我身边。问题是数据有时可以插入到数据库但现在甚至认证部分也无法插入所以我想知道如何解决这个问题?

public class Register extends AppCompatActivity {

private FirebaseAuth auth;
private EditText run, rpw,re;
private EditText rpn, ra, rpc;
private TextView rage;
private String usnm, pswd, email, age, uid;
private FirebaseDatabase db;
private DatabaseReference ref;
private DatePickerDialog.OnDateSetListener dsl;

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

    auth = FirebaseAuth.getInstance();
    db = FirebaseDatabase.getInstance();
    ref = db.getReference();
    FirebaseUser ur = auth.getCurrentUser();
    uid = ur.getUid();//getuser id

    run = (EditText) findViewById(R.id.Run);
    rpw = (EditText) findViewById(R.id.Rpw);
    rage = (TextView) findViewById(R.id.Rage);

    rage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Calendar mc = Calendar.getInstance();
            int y = mc.get(Calendar.YEAR);
            int m = mc.get(Calendar.MONTH);
            int day = mc.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dpd = new DatePickerDialog(Register.this,
                    android.R.style.Theme_Holo_Dialog_MinWidth, dsl, y, m, day);

            dpd.getDatePicker().setMaxDate(mc.getTimeInMillis());
            dpd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dpd.show();
        }
    });

    dsl = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            month = month+1;

            String date = dayOfMonth +"/" + month + "/" +year;
            rage.setText(date);
        }
    };

    re = (EditText) findViewById(R.id.Re);
    rpn = (EditText) findViewById(R.id.Rpn);
    ra = (EditText) findViewById(R.id.Ra);
    rpc = (EditText) findViewById(R.id.Rpc);

    //rbstd = (RadioButton) findViewById(R.id.rbstd);
    //rbtt= (RadioButton) findViewById(R.id.rbtt);

    final Button br = (Button) findViewById(R.id.Rbt);

    br.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            usnm = run.getText().toString().trim();
            pswd = rpw.getText().toString().trim();
            email = re.getText().toString().trim();
            age = rage.getText().toString().trim();

            validateUsername(usnm);

            if(TextUtils.isEmpty(pswd)){
                Toast.makeText(Register.this, "Password cannot be empty", LENGTH_SHORT).show();
                return;

            }else if (pswd.length()<8){
                Toast.makeText(Register.this, "Password cannot less then 8 characters", LENGTH_SHORT).show();
                return;

            }else if (pswd.length()>12){
                Toast.makeText(Register.this, "Password cannot greater then 12 characters", LENGTH_SHORT).show();
                return;

            }else if (TextUtils.isEmpty(age)){
                Toast.makeText(Register.this, "Please select date", LENGTH_SHORT).show();
                return;

            }

            validateEmail(email);

            register(usnm, pswd, age, email);

        }//end of onclick
    });


}

private void register (final String un, final String pw, final String ag, final String em){
    Task<AuthResult> authResultTask = auth.createUserWithEmailAndPassword(em, pw).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if (task.isSuccessful()) {

                //insertion
                User u = new User (un, pw, ag, em);
                ref.child(uid).setValue(u);

                //prompt message
                Toast.makeText(Register.this, "Success Registration", Toast.LENGTH_LONG).show();
                Intent i = new Intent(Register.this, StudentHomePage.class);
                startActivity(i);
                finish();

            } else {

                Toast.makeText(Register.this, "Registration Error", Toast.LENGTH_LONG).show();
                Intent i = new Intent(Register.this, Login.class);
                startActivity(i);
                finish();
            }

        }
    });


}// end of signup

private void validateUsername(final String validateName){

    if (TextUtils.isEmpty(validateName)){
        Toast.makeText(Register.this, "Username cannot be empty", LENGTH_SHORT).show();
        return;

    }else if (validateName.length()<8){
        Toast.makeText(Register.this, "Username cannot less then 8 characters", LENGTH_SHORT).show();
        return;

    }else if (validateName.length()>12){
        Toast.makeText(Register.this, "Username cannot greater then 12 characters", LENGTH_SHORT).show();
        return;

    }else {


            ref.child(uid).orderByChild("username").equalTo(validateName).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    if (dataSnapshot!=null && dataSnapshot.getChildren()!=null && dataSnapshot.hasChild(validateName)){

                        Toast.makeText(Register.this, "Username had been taken", LENGTH_SHORT).show();
                        return;

                    }else{

                        Toast.makeText(Register.this, "Username unique", LENGTH_SHORT).show();
                        return;
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(Register.this, "Database Connection Error", LENGTH_SHORT).show();
                    return;
                }
            });


    }
}

private void validateEmail(final String valiemail){
    if (TextUtils.isEmpty(valiemail)){
        Toast.makeText(Register.this, "Username cannot be empty", LENGTH_SHORT).show();
        return;

    }else {

        ref.child(uid).orderByChild("email").equalTo(valiemail).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot!=null && dataSnapshot.getChildren()!=null && dataSnapshot.hasChild(valiemail)){

                    Toast.makeText(Register.this, "Email had been taken", LENGTH_SHORT).show();
                    return;

                }else{

                    Toast.makeText(Register.this, "Email unique", LENGTH_SHORT).show();
                    return;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(Register.this, "Database Connection Error", 
LENGTH_SHORT).show();
                 return;
            }
        });

    }
}

}

logcat显示

  

java.lang.NullPointerException:不能为参数&#39; pathString&#39;

传递null

请指教我解决这个问题。 当我想插入时,应用程序停止工作,现在整个注册页面都无法显示。

2 个答案:

答案 0 :(得分:0)

您正在uid中阅读用户及其onCreate()

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

    auth = FirebaseAuth.getInstance();
    db = FirebaseDatabase.getInstance();
    ref = db.getReference();
    FirebaseUser ur = auth.getCurrentUser();
    uid = ur.getUid();//getuser id

创建活动时无法保证有经过身份验证的用户。这就是您可以在身份验证状态更改时附加侦听器的原因。通过倾听,您可以对用户登录或退出的时间作出反应。

auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
        if (firebaseAuth.getCurrentUser() != null) {
            Register.this.uid = firebaseAuth.getCurrentUser().getUid();
        }
        else {
            Register.this.uid = null;
        }
    }
};

答案 1 :(得分:0)

因为我没有足够的声誉来评论我对此的建议是

  • 在获取之前你应该检查 if(ur!= null),如果它为null则需要更多调试(确保用户已登录)

  • 其次将firebase库更新到最新版本我不知道哪个版本但我遇到了getCurentUser()返回null,除非你注销并登录两次。但幸运的是它目前已修复。 我希望这有助于某人!