从Firebase身份验证中检索用户ID时,我得到一个空值

时间:2019-01-23 11:06:32

标签: java android firebase firebase-realtime-database

我正在尝试从Firebase身份验证获取用户UID,但它给了我一个空值。我有一个管理员,它将注册许多学校,每个学校都有自己的ID(通过按键)。管理员将根据需要重新注册学校,他们只有在注册学校后才能登录。

this is the firebase realtime

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

        MngEmail = (EditText)findViewById(R.id.MngEmailField);
        MngPassword = (EditText)findViewById(R.id.mngPass);
        MngFrgtPass = (TextView)findViewById(R.id.MngForText);
        MngLoginBtn = (Button)findViewById(R.id.MngBtn);
        loadingbar = new ProgressDialog(this);

        MngAuth = FirebaseAuth.getInstance();
        SuperAdminUId = MngAuth.getCurrentUser().getUid();
        String key = MngRef.child("SchoolData").child(SuperAdminUId).getKey();

        MngRef = FirebaseDatabase.getInstance().getReference().child("SchoolData").child(SuperAdminUId).child(key);

        MngFrgtPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                TO Dooo
            }
        });

        MngLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Query query = MngRef.orderByChild("SchoolEmail").equalTo(MngEmail.getText().toString().trim());
                        query.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.exists()) {
                                    // dataSnapshot is the "issue" node with all children with id 0

                                    for (DataSnapshot user : dataSnapshot.getChildren()) {
                                        // do something with the individual "issues"
                                        School schoolinfo = user.getValue(School.class);

                                        if (schoolinfo.equals(MngPassword.getText().toString().trim())) {
                                            sendMngToMain();
                                        } else {
                                            Toast.makeText(login.this, "Password is wrong", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                } else {
                                    Toast.makeText(login.this, "User not found", Toast.LENGTH_LONG).show();
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行的顺序:

String key = MngRef.child("SchoolData").child(SuperAdminUId).getKey();
MngRef = FirebaseDatabase.getInstance().getReference().child("SchoolData").child(SuperAdminUId).child(key);

MngRef = FirebaseDatabase.getInstance().getReference().child("SchoolData").child(SuperAdminUId).child(key);
String key = MngRef.child("SchoolData").child(SuperAdminUId).getKey();

因此,您遇到该异常是因为尝试使用尚未初始化的对象。