Android Firebase - cant get userid using getUid()

2020-03-01 15:59发布

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:

firebaseRef.getAuth().getUid()

I am receiving an error of within the app emulator "unfortunately, app has stopped".

This looks like the main error I am getting in android studio in the monitor: Something to do with the getUid().

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.firebase.client.AuthData.getUid()' on a null object reference

Here is my activity code:

package me.netsync.netconnect;

import android.app.ActionBar;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;

public class SettingActivity extends AppCompatActivity {

    private Firebase mRef;
    private Button BtnSettings;
    private EditText TxtUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        mRef = new Firebase(Constants.FIREBASE_URL);

        BtnSettings = (Button)findViewById(R.id.BtnSettings);
        BtnSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // username txt update the profile
                TxtUsername = (EditText)findViewById(R.id.TxtUsername);
                String TxtUsernameStr = TxtUsername.getText().toString();
                TxtUsernameStr = TxtUsernameStr.trim();

                // check we can get a userid
               /* if(mRef.getAuth() == null){
                    Intent intent = new Intent(SettingActivity.this,LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }*/

                if(TxtUsernameStr.isEmpty()){
                    // show a dialog box
                    AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                    builder.setMessage("Please make sure you enter a Username" + TxtUsernameStr)
                            .setTitle("Error!")
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }else{
                    Firebase upDateUserProfile = new Firebase(Constants.FIREBASE_URL);
                    String uid = upDateUserProfile.getAuth().getUid();
                    User user = new User(TxtUsernameStr);
                    upDateUserProfile.child("users").child(uid).child("username").setValue(user);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int btn = item.getItemId();

        if(btn == R.id.action_logout){
            mRef.unauth();
            Intent intent = new Intent(this,LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

        if(btn == R.id.action_settings){
            mRef.unauth();
            Intent intent = new Intent(this,SettingActivity.class);
            startActivity(intent);
        }

        return true;
    }
}

Any pointers appreciated.

5条回答
Luminary・发光体
2楼-- · 2020-03-01 16:09

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

查看更多
▲ chillily
3楼-- · 2020-03-01 16:13

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

enter image description here

查看更多
Bombasti
4楼-- · 2020-03-01 16:17

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

            }

        }
    };
查看更多
Root(大扎)
5楼-- · 2020-03-01 16:19

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();
}
查看更多
放我归山
6楼-- · 2020-03-01 16:20
  • 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");
    
查看更多
登录 后发表回答