Ich verwende Firebase für die Benutzerauthentifizierung. Wenn sich ein Nutzer anmeldet, möchte ich, dass ihm ein Anzeigename mit seinem Konto zugewiesen wird.Einstellung von displayName während der Registrierung
Im Moment verwende ich einen generischen Anzeigenamen, "Jane Q. Users", den ich nach einer erfolgreichen Registrierung der E-Mail/Passwort-Kombination eingestellt habe.
Wenn ich versuche, dies zu tun es die folgende schwerwiegende Ausnahme geben:
01-11 14:16:37.640 11076-11076/com.snapwebdevelopment.scanhappy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.snapwebdevelopment.scanhappy, PID: 11076
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:143)
at com.snapwebdevelopment.scanhappy.SignupActivity$1.onComplete(SignupActivity.java:105)
at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Mein SignupActivity.class
wie folgt aussieht:
package com.snapwebdevelopment.scanhappy;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserInfo;
import com.google.firebase.auth.UserProfileChangeRequest;
public class SignupActivity extends Activity implements View.OnClickListener {
//defining view objects
private EditText editTextEmail;
private EditText editTextPassword;
private Button buttonSignup;
private ProgressDialog progressDialog;
//defining firebaseauth object
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
//initializing firebase auth object
firebaseAuth = FirebaseAuth.getInstance();
//initializing views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
buttonSignup = (Button) findViewById(R.id.buttonSignup);
progressDialog = new ProgressDialog(this);
//attaching listener to button
buttonSignup.setOnClickListener(this);
}
private void registerUser(){
//getting email and password from edit texts
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
//checking if email and passwords are empty
if(TextUtils.isEmpty(email)){
Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
return;
}
//if the email and password are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
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("add name", "User profile updated.");
}
}
});
Log.d("displayname", user.getDisplayName());
Toast.makeText(SignupActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
startActivity(new Intent(SignupActivity.this, MainActivity.class));
}else{
//display some message here
Toast.makeText(SignupActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
@Override
public void onClick(View view) {
//calling register method on click
registerUser();
}
}
Und mein activity_signup.xml
wie folgt aussieht:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.snapwebdevelopment.scanhappy.SignupActivity">
<LinearLayout
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="UselessParent">
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:hint="@string/enter_email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:hint="Enter password (6+ characters)"
android:inputType="textPassword"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/buttonSignup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Signup"
tools:ignore="HardcodedText" />
</LinearLayout>
</RelativeLayout>
Was mache ich diesen Fehler? Was kann ich in meinem Code ändern, um den Benutzernamen eines neuen Accounts erfolgreich als "Jane Q. User" zu registrieren?
Ich denke, dass Sie den Namen korrekt aktualisieren, aber Ihre Anmeldung in Zeile 105 wird zu früh aufgerufen. Deshalb ist Ihre Fehlermeldung eine 'NullPointerException'. Der Anzeigename ist noch nicht festgelegt, wenn Sie ihn aufrufen. Versuchen Sie, das Protokoll in Zeile 105 innerhalb des 'OnCompleteListener' in Zeile 97 zu verschieben. – hotrod
Dies steht in keinem Zusammenhang mit dieser Frage, aber warum nicht FirebaseUI für die Behandlung des gesamten Benutzeranmelde-/Registrierungsflusses für Sie verwenden? https://github.com/firebase/FirebaseUI-Android/tree/master/auth –
@HarshitDwivedi Ich habe diese Option definitiv notiert, aber ich würde gerne in der Lage sein, zusätzliche Felder als das, was verfügbar ist, hinzuzufügen und das Aussehen anzupassen und fühle mich ein bisschen weiter. – beckah