【问题标题】:Load FirebaseUser info gives null value加载 FirebaseUser 信息给出空值
【发布时间】:2017-07-30 14:25:31
【问题描述】:

在加载 FirebaseUser 信息的 showdata 方法中,我的 uInfo 值为空,我运行应用程序并使用 User_02 登录,调试显示: ds.getChild(userId) 到达登录并使用正确密钥加载的正确用户,但是当尝试从 UserInformations.class 获取值时给我 NullPointerException ,那么代码中的错误是什么?

我不认为 ds 具有 User_01 的键和值。这正常吗?

public class AddToDatabase extends AppCompatActivity {
    private static final String TAG = "AddToDatabase";
    private ListView userInfolist ;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    String userId;
    UserInformations uInfo =new UserInformations();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_to_database);
        userInfolist = (ListView) findViewById(R.id.userInfolist);
        mAuth = FirebaseAuth.getInstance();
        FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference myRef = mFirebaseDatabase.getReference();
        FirebaseUser user = mAuth.getCurrentUser();
        userId = user.getUid();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    toastMassege(" You are successfly signed in with " + user.getEmail());
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                    toastMassege(" You are successfly signed out ");
                }
            }
        };

        // Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                 showData(dataSnapshot);
            }
            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value

            }
        });
    }

      private void showData(DataSnapshot dataSnapshot) {
      for(DataSnapshot ds : dataSnapshot.getChildren()){


            uInfo.setName(ds.child(userId).getValue(UserInformations.class).getName());            // set the name
            uInfo.setEmail(ds.child(userId).getValue(UserInformations.class).getEmail());          // set the Email
            uInfo.setPhone_num(ds.child(userId).getValue(UserInformations.class).getPhone_num());  // set the PhonNum

            ArrayList<String> arrayList =new ArrayList<>();
            arrayList.add(uInfo.getName());
            arrayList.add(uInfo.getEmail());
            arrayList.add(uInfo.getPhone_num());
            ArrayAdapter<String> adapter =new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList);
            userInfolist.setAdapter(adapter);
      }
    }
    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }
    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }
    private void toastMassege(String massege) {
        Toast.makeText(this, massege, Toast.LENGTH_SHORT).show();
    }
}

UserInformation.java

class UserInformations {

    private String Email,Name,Phone_num ;

    UserInformations() {
    }

    String getEmail() {
        return Email;
    }

    void setEmail(String email) {
        this.Email = email;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        this.Name = name;
    }

    String getPhone_num() {
        return Phone_num;
    }

    void setPhone_num(String phone_num) {
        this.Phone_num = phone_num;
    }

}

【问题讨论】:

  • 你找到答案了吗?
  • 不,没有,我尝试了答案 1,但它不起作用,给出相同的错误并且 uInfo 仍然具有空值,我使用 User_03 登录,userId 是正确的 Id,但 ds 有 User_01 key 和 values ,然后 ds.getValue(UserInformations.class).getName() == User_01 name
  • 我已经在下面发布了我的答案。

标签: android firebase firebase-realtime-database nullpointerexception


【解决方案1】:

首先,将 UserInformations 中的变量从 private 更改为 public

class UserInformations {

    public String Email,Name,Phone_num ;

}

其次,您必须将 Firebase 路径从 Phone number 更改为 Phone_num。为什么?嗯,那是因为你在 UserInformation 中声明了 String Phone_num

您当前的 JSON:

{
  "uid001": {
    "Email": "user01@gmail.com",
    "Name": "user01",
    "Phone number": "0123456789"
  },
  "uid002": {
    "Email": "user02@gmail.com",
    "Name": "user02",
    "Phone number": "0123456789"
  },
}

改为以下 JSON:

{
  "uid001": {
    "Email": "user01@gmail.com",
    "Name": "user01",
    "Phone_num": "0123456789"
  },
  "uid002": {
    "Email": "user02@gmail.com",
    "Name": "user02",
    "Phone_num": "0123456789"
  },
}

【讨论】:

  • 它给了我错误:com.google.firebase.database.DatabaseException: 发现两个 getter 或属性的区分大小写冲突的字段:名称
  • 表示前面的错误解决了。现在你有另一个错误。唔。你能把它包括在问题中吗?错误的完整跟踪
  • 我昨天问了这个项目的问题,没有答案,你能看看吗??stackoverflow.com/questions/45425921/…>
【解决方案2】:

当您使用for(DataSnapshot ds : dataSnapshot.getChildren()) 时,ds 已经有了作为用户节点的子节点的数据,不需要child(userId) 直接这样做:

uInfo.setName(ds.child("name").getValue()); 
uInfo.setEmail(ds.child("email").getValue());
uInfo.setPhone_num(ds.child("phone_num").getValue());

【讨论】:

  • 它不起作用,给出相同的错误并且 uInfo 仍然有 null 值,我使用 User_03 登录,userId 是正确的 Id,但是 ds 有 User_01 键和值,然后 ds.getValue(UserInformations .class).getName() == User_01 名称,
  • 我很感谢你的努力,但它没有奏效并给出同样的错误。
猜你喜欢
  • 2019-03-24
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 2016-02-22
相关资源
最近更新 更多