Java 我想从firestore检索当前记录的用户数据,但显示错误

Java 我想从firestore检索当前记录的用户数据,但显示错误,java,android,firebase-realtime-database,google-cloud-firestore,Java,Android,Firebase Realtime Database,Google Cloud Firestore,尝试在空对象引用上调用虚拟方法com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser': MyAccount.java 将onCreate方法代码更改为: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

尝试在空对象引用上调用虚拟方法com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser':

MyAccount.java

将onCreate方法代码更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);

    email = (TextView)findViewById(R.id.email_txt);
    mAuth = FirebaseAuth.getInstance();
    UserId = mAuth.getCurrentUser().getUid();
    passwordtxt = (TextView)findViewById(R.id.pass_txt);

    mFirestore = FirebaseFirestore.getInstance();
   mFirestore.collection("Hospitals").document(UserId).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            String user_name = documentSnapshot.getString("email");
            String password  = documentSnapshot.getString("password");


            email.setText(user_name);
            passwordtxt.setText(password);
        }
    });    
}

兄弟,我找不到你的代码和我的代码有什么区别只要复制并粘贴到你的代码中你就会找到它。。。如您所见,mAuth=FirebaseAuth.getInstance;必须在使用它之前初始化…@SharmaBlog您需要获取实例1st mAuth=FirebaseAuth.getInstance,然后获取用户id UserId=mAuth.getCurrentUser.getUid;仔细检查..谢谢,兄弟working@SharmaBlog欢迎快乐编码..:upvote将被应用。@SantanuSur它不在下面,因为你可以复制…mAuth为空@GowthamanM它显然是一个复制品@SantanuSur是的,我知道..但是当你尽可能复制一个与android相关的标签时。如果没有添加一个合适的标签,问题应该用该标签编辑,然后应该标记为这样@戈特哈曼
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);

    email = (TextView)findViewById(R.id.email_txt);
    mAuth = FirebaseAuth.getInstance();
    UserId = mAuth.getCurrentUser().getUid();
    passwordtxt = (TextView)findViewById(R.id.pass_txt);

    mFirestore = FirebaseFirestore.getInstance();
   mFirestore.collection("Hospitals").document(UserId).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            String user_name = documentSnapshot.getString("email");
            String password  = documentSnapshot.getString("password");


            email.setText(user_name);
            passwordtxt.setText(password);
        }
    });    
}