英文:
Cannot resolve final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
问题
无法解决:
final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
如果我将其更改为:
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
它将无法解决这里的 getUid
:
if (!user.getUser_id().equals(firebaseUser.getUid())){
mUsers.add(user);
如果我将其更改为 FirebaseUser firebaseUser = FirebaseUser.getInstance();
,它将无法解决 getInstance()
;我该怎么做才能消除这个错误,请帮忙。
private void readUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
assert user !=null;
assert firebaseUser !=null;
if (!user.getUser_id().equals(firebaseUser.getUid())){
mUsers.add(user);
}
}
userAdapter = new UserAdapter(getContext(), mUsers);
recyclerView.setAdapter(userAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
英文:
Cannot reolve
final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
If I change it to
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
it won't resolve the getUid in this
if (!user.getUser_id().equals(firebaseUser.getUid())){
mUsers.add(user);
And if I change it to FirebaseUser firebaseUser = FirebaseUser.getInstance();
it wont resolve the getInstance(); What can I do to remove the error please help
private void readUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
assert user !=null;
assert firebaseUser !=null;
if (!user.getUser_id().equals(firebaseUser.getUid())){
mUsers.add(user);
}
}
userAdapter = new UserAdapter(getContext(), mUsers);
recyclerView.setAdapter(userAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案1
得分: 1
FirebaseAuth.getInstance()
返回一个 FirebaseAuth
实例,因此这段代码:
final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
应该改成这样:
final FirebaseAuth auth = FirebaseAuth.getInstance();
然后将这段代码:
assert user != null;
assert firebaseUser != null;
if (!user.getUser_id().equals(firebaseUser.getUid())) {
mUsers.add(user);
}
改成这样:
if (!user.getUser_id().equals(auth.getCurrentUser().getUid())) {
mUsers.add(user);
}
英文:
FirebaseAuth.getInstance()
returns an instance of FirebaseAuth
, so this:
final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
should be this instead:
final FirebaseAuth auth = FirebaseAuth.getInstance();
Then change this:
assert user !=null;
assert firebaseUser !=null;
if (!user.getUser_id().equals(firebaseUser.getUid())){
mUsers.add(user);
}
into this:
if (!user.getUser_id().equals(auth.getCurrentUser().getUid())){
mUsers.add(user);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论