Android Firebase - cant get userid using getUid() - Error: on null object reference

后端 未结 5 805
南笙
南笙 2021-01-02 06:23

I am using Firebase and have a signup / login activities working fine but I also want each user to update a username. It seems I am unable to run:

firebaseR         


        
相关标签:
5条回答
  • 2021-01-02 07:06

    Need to add AuthStateListener.

            mAuth=FirebaseAuth.getInstance();
            mAuthListener=new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
    
    
                if(firebaseAuth.getCurrentUser()==null){
    
                   //Your action here
    
                }
    
            }
        };
    
    0 讨论(0)
  • 2021-01-02 07:13

    A simple way to check if there is any update for Firebase dependency:

    enter image description here

    0 讨论(0)
  • 2021-01-02 07:14

    FirebaseAuth.getInstance().getUid(); This API seems to have been removed in Firebase 12.0.0

    Use this instead FirebaseAuth.getInstance().getCurrentUser().getUid();

    with the appropriate null check, or use Firebase 12.0.1 where i now seems to have @Hide annotation

    0 讨论(0)
  • 2021-01-02 07:16
    • Add this after the Main Activity class

      Firebase firebaseAuth;
      DatabaseReference reference;
      
    • Add anywhere in onCreate

      String usern;
      usern = firebaseAuth.getInstance().getCurrentUser().getUid();
      
    • Upload to Firebase to their repective children

      reference=FirebaseDatabase.getInstance()
                .getReference().child("Patients")
                .child("Patient" + usern).child("Pres");
      
    0 讨论(0)
  • 2021-01-02 07:18

    In your app build.gradle, add this to your dependencies

    compile 'com.google.firebase:firebase-auth:9.4.0'

    import com.google.firebase.auth.FirebaseAuth;
    ...
    ...
    
    if (FirebaseAuth.getInstance().getCurrentUser() == null) {
        //Go to login 
    }
    else{
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    }
    
    0 讨论(0)
提交回复
热议问题