如何在Android上使用Firebase创建使用电子邮件和密码的用户时添加用户的显示名称

时间:2016-10-28 22:47:31

标签: android firebase

我正在尝试使用Android上的Firebase使用createUserWithEmailAndPassword()方法创建用户时添加用户的显示名称。

当我使用以下代码更新用户的个人资料时,我会收到如此异常:com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.FirebaseException: An internal error has occured. [ USER_NOT_FOUND ]

Task未成功完成

displayName变量也为空。

代码是:

public class MainActivity extends AppCompatActivity {

private FirebaseAuth firebaseAuth;
private FirebaseUser user;
private Dialog dialog;
private final String TAG = "MainActivity";

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

    // Get FireBase FireBaseAuth instance
    firebaseAuth = FirebaseAuth.getInstance();
    String displayName = firebaseAuth.getCurrentUser().getDisplayName();

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

    TextView textView = (TextView) findViewById(R.id.txtMain);
    textView.setText(displayName);
    Log.i(TAG, "Display name = " + displayName);


    if (user.getDisplayName() == null) {
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.update_user_dialog);
        dialog.setTitle("Update your profile...");

        // set the custom dialog components - text, image and button
        EditText txtName = (EditText) dialog.findViewById(R.id.txtName_user_dialog);
        final String name = txtName.getText().toString().trim();

        Button dialogButton = (Button) dialog.findViewById(R.id.btnUpdate_user_dialog);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName(name)
                        .build();

                user.updateProfile(profileUpdates)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    dialog.dismiss();
                                    Toast.makeText(getApplicationContext(), "Your profile has been updated.", Toast.LENGTH_SHORT).show();
                                    Log.i(TAG, "Profile has been updated.");
                                } else
                                    Log.i(TAG, "Profile has not been updated. " + task.getResult());
                            }
                        });
            }
        });

        dialog.show();
    }

}
}

我最终想要完成的是在创建用户时添加用户显示名称。

0 个答案:

没有答案