无法从Firebase检索用户数据

时间:2018-09-24 21:26:11

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

我具有以下firebase结构:

Users" : {
"angelbreath" : {
      "PvP_Wins" : 0,
      "PvP_scores" : 0,
      "avatar" : "https://i.imgur.com/qp9gnKE.png",
      "gender" : "Male",
      "userName" : "angelbreath",
      "user_class" : "Bard",
      "user_id" : "sC8JGw6SvMUGH4Id1HwcSf6Sl5n1"
    },  

由于空异常,我正在尝试获取当前身份验证用户的数据,但是没有运气。经过多次尝试,我最终得到了以下代码:

private void showData() {
            DatabaseReference Users = database.getReference("Users");
            final Query userQuery = Users.orderByChild("user_id");
            final FirebaseUser user = firebaseAuth.getCurrentUser();

            userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot post : dataSnapshot.getChildren() ){
                        if(post.child("user_id").getValue().equals(user.getUid())){
                            Log.d("Output", "Found");
                            Log.d("Output", post.getKey().toString());

                            userClass.setText(post.child("user_class").getValue().toString());
                            userGender.setText(post.child("gender").getValue().toString());
                        }else{
                            Log.d("Output", "Failure");
                        }

                        Log.d("Output", post.child("user_id").toString());
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

据我所知,因为我是Firebase的新手,所以我有正确的引用,也以正确的方式查询。我不知道为什么我得到例外。 万一我也有以下检查:

//Checking whether a user as already Logged In
        if(firebaseAuth.getCurrentUser()==null){
            finish();
            //Starting the User Login Activity if the user is not Logged in
            startActivity(new Intent(this,MainActivity.class));
        }

所以我想我拥有当前用户和他的ID,所以我用用户ID查询Users。怎么了?

我将不胜感激!

编辑错误日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String eu.healthydev.quizhero.Model.User.getUserName()' on a null object reference
        at eu.healthydev.quizhero.UserProfile$1.onDataChange(UserProfile.java:88)
        at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.2:75)
        at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.2:63)
        at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.2:55)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)

Java:88

if(post.child("user_id").getValue().equals(user.getUid())){

用户模型:

public class User {

    int PvP_wins, PvP_score;
    private String user_class, user_gender, userName;
    private String avatar, userid;

    public User() {
    }

    public User(int pvP_wins, int pvP_score, String user_class, String user_gender, String userName, String avatar, String userid) {
        PvP_wins = pvP_wins;
        PvP_score = pvP_score;
        this.user_class = user_class;
        this.user_gender = user_gender;
        this.userName = userName;
        this.avatar = avatar;
        this.userid = userid;
    }

    public int getPvP_wins() {
        return PvP_wins;
    }

    public void setPvP_wins(int pvP_wins) {
        PvP_wins = pvP_wins;
    }

    public int getPvP_score() {
        return PvP_score;
    }

    public void setPvP_score(int pvP_score) {
        PvP_score = pvP_score;
    }

    public String getUser_class() {
        return user_class;
    }

    public void setUser_class(String user_class) {
        this.user_class = user_class;
    }

    public String getUser_gender() {
        return user_gender;
    }

    public void setUser_gender(String user_gender) {
        this.user_gender = user_gender;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

2 个答案:

答案 0 :(得分:0)

放置点“。”在firebaseAuth.getCurrentUser()之后将自动显示哪些方法可用于获取当前用户数据,并且如果您仅想获取uid,则不需要通过侦听器获取此类数据。(在这种情况下)

例如:

FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
    String uid = firebaseUser.getUid();

    Log.d("Tag" + uid)
}

此外,在放置点之后,它可能会显示当前节点中要检索的其他项目。关键是,当您可以像上面的代码一样检索那些项时,我真的看不到在这里使用Listener的原因。

答案 1 :(得分:0)

问题是,方法UserProfile.onDataChange()的{​​{1}}是user

除非看过类NULL以及如何实例化,否则很难讲更多。

您可以用不同的方式初始化字段(这不会解决任何问题,但可以防止UserProfile):

NPE