这是我的方法,用于将配置文件数据提取到用户对象中,然后将此用户设置为MainActivity静态currentUser对象,但在这里currentUser提供的是null。
注意:onSuccess方法中的用户已成功获取。
private static User currentUser;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
new FirebaseInteract(this).fetchProfile(cUser);
// this toast gives nullpointer exception
Toast.makeText(this, currentUser.getSemester() +"",Toast.LENGTH_SHORT).show();}
public void fetchProfile(FirebaseUser fUser) {db.collection("users").document(fUser.getEmail()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
user = documentSnapshot.toObject(User.class);
Toast.makeText(activity, "Successfully fetched user profile", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, user.getRole() + "", Toast.LENGTH_SHORT).show();
MainActivity.setCurrentUser(user);
}
}).addOnFailureListener(onFailureListener);
}