Why do I only get null from getPhotoUrl from a FirebaseUser object?

1

This is the code where I'm using the object of type FirebaseUser

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Toast.makeText(Login.this,"Bienvenido: "+user.getPhotoUrl().toString(),Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Navigation.class);
intent.putExtra(Navigation.users,user.getDisplayName());
intent.putExtra(Navigation.correos,user.getEmail());
intent.putExtra(Navigation.urlsPerfil,user.getPhotoUrl().toString());
startActivity(intent);

This is the error that throws me

 Process: com.fundamentos.tomy.helpmyfido, PID: 5959
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
    at com.fundamentos.tomy.helpmyfido.Login$2$1.onComplete(Login.java:92)
    at com.google.android.gms.tasks.zzj.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5422)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

I'm using the methods I saw in the Firebase documentation, but I only have this error when I deploy them

    
asked by Abisur Diaz Ramirez 17.12.2018 в 10:40
source

1 answer

0

If you use a provider such as Gmail to register or log in a user, the provider gives you the name of the user and the photo, but if the login is with Firebase directly with email and password you will not have available with FirebaseUser those options, for that you will have to use UserProfileChangeRequest , then when you register a user you will have to make them take or choose a photo to create that user with a photo already assigned, and then set that information to use it with FirebaseUser

For example to set getDisplayName and getPhotoUrl , this you do in the registration window

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Jane Q. User")
        .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
        .build();

user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User profile updated.");
                }
            }
        });

I leave the link for you to see link

After making that change in the next login and you can use these options without problems, keep in mind that the best place to assign this data is the window where you register the user. You can do it after the login too, it's the same, the null that you throw is why you do not find anything associated with that user.

    
answered by 17.12.2018 / 16:02
source