我似乎无法将文本放入 TextView 中。数据来自 Firestore 数据库。当我运行它时,TextView 是空白的并返回 null。以下是我到目前为止所做的事情。有人可以看一下吗?你可以在这里看到我的数据库结构:[数据库结构图][1]
public class ProfileFragment extends Fragment {
TextView firstNameText, age;
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
firstNameText = rootView.findViewById(R.id.profile_firstName);
age = rootView.findViewById(R.id.profile_age);
getUserInfo();
return rootView;
}
public void getUserInfo() {
db.collection("Users").document(mAuth.getCurrentUser().getEmail()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
String firstName = documentSnapshot.getString("Firstname");
Log.d(TAG, "Printing docData: " + documentSnapshot.getData());
firstNameText.setText(firstName);
}
}
});
}
}
最佳答案
我猜是这样的
mAuth.getCurrentUser().getEmail()
就是这里的问题。尝试将用户的电子邮件放入字符串中
mAuth = FirebaseAuth.getInstance();
final FirebaseUser theUser = mAuth.getCurrentUser();
if (theUser !=null)
String UID = theUser.getEmail().toString();
并替换
mAuth.getCurrentUser().getEmail()
带有 UID 。
关于java - 在 Firestore 中找不到文档数据,返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071016/