我正在尝试使用auth UID
作为子节点将用户添加到Firebase中的实时数据库。
我一直在用这个问题作为指导:
在调试模式下运行项目时,用户会被添加。然而,当我正常运行时,应用程序崩溃了。我得到的错误是空指针异常。
这是我的代码:
public class RegisterUser extends AppCompatActivity {
private static final String TAG = "RegisterUser";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseUser fbUser =null;
private DatabaseReference mFirebaseDatabase;
User user;
private EditText mEmail, mPassword;
private RadioButton rManager, rPlayer;
private Button mRegister;
private ProgressBar mProgressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_user);
mEmail = (EditText) findViewById(R.id.email_field);
mPassword = (EditText) findViewById(R.id.password_field);
rManager = (RadioButton) findViewById(R.id.managerBtn);
rPlayer = (RadioButton) findViewById(R.id.playerBtn);
mRegister = (Button) findViewById(R.id.btnSignIn);
mProgressbar = (ProgressBar) findViewById(R.id.progressBar);
mAuth = FirebaseAuth.getInstance();
mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
if (!rManager.isChecked() && !rPlayer.isChecked()) {
Toast.makeText(RegisterUser.this, "You must select either player or manager", Toast.LENGTH_LONG).show();
}
mProgressbar.setVisibility(View.VISIBLE);
String type = "";
//radio buttons
if (rManager.isChecked()) {
type = "Manager";
} else if (rPlayer.isChecked()) {
type = "Player";
}
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("User");
String userId = mFirebaseDatabase.push().getKey();
User user = new User(email, password, type);
mAuth.createUserWithEmailAndPassword(email, password);
fbUser = FirebaseAuth.getInstance().getCurrentUser();
mFirebaseDatabase.child(fbUser.getUid()).setValue(user);
mProgressbar.setVisibility(View.GONE);
startActivity(new Intent(RegisterUser.this, HomePage.class));
@Override
protected void onResume() {
super.onResume();
mProgressbar.setVisibility(View.GONE);
}
public void onLogIn(View v) {
Intent i = new Intent(this, MainActivity.class);
//i.putExtra(key_1, userName.getText().toString()); //get id for training and display details in full
startActivity(i);
}
}
错误:
01-18 17:01:42.331 3781-3781/com.example.cianm.fyp2018v2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cianm.fyp2018v2, PID: 3781
java.lang.NullPointerException
at com.example.cianm.fyp2018v2.RegisterUser$1.onClick(RegisterUser.java:142)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18789)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
非常感谢任何帮助。
答案 0 :(得分:0)
通过使用onAuthStateChanged
我能够获取当前用户UID
并将其用作User对象的子节点。
代码:
import android.content.Intent;
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.ProgressBar;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.cianm.testauth.Entity.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RegisterUser extends AppCompatActivity {
private static final String TAG = "MainActivity";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mFirebaseDatabase;
private EditText mEmail, mPassword;
private Button mRegister;
private RadioButton rManager, rPlayer;
private ProgressBar mProgressbar;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_user);
mEmail = (EditText) findViewById(R.id.emailField);
mPassword = (EditText) findViewById(R.id.passwordField);
rManager = (RadioButton) findViewById(R.id.radioManager);
rPlayer = (RadioButton) findViewById(R.id.radioPlayer);
mRegister = (Button) findViewById(R.id.registerBtn);
mProgressbar = (ProgressBar) findViewById(R.id.progressBar);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
startActivity(new Intent(RegisterUser.this, HomePage.class));
}
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser fbUser = firebaseAuth.getCurrentUser();
if (fbUser != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in" + fbUser.getUid());
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("User");
mFirebaseDatabase.child(fbUser.getUid()).setValue(user);
Toast.makeText(getApplicationContext(), "Sucessfully signed in with: " + fbUser.getEmail(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterUser.this, HomePage.class));
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
if (!rManager.isChecked() && !rPlayer.isChecked()) {
Toast.makeText(RegisterUser.this, "You must select either player or manager", Toast.LENGTH_LONG).show();
}
mProgressbar.setVisibility(View.VISIBLE);
String type = "";
//radio buttons
if (rManager.isChecked()) {
type = "Manager";
} else if (rPlayer.isChecked()) {
type = "Player";
}
mProgressbar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password);
user = new User(email, password, type);
}
});
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
@Override
protected void onResume() {
super.onResume();
mProgressbar.setVisibility(View.GONE);
}
}
答案 1 :(得分:0)
使用电子邮件和passwoed创建用户后,您需要使用mAuth.signInWithEmailAndPassword登录用户 我认为你的getcurrentuser.getuid()返回null,因此返回零点异常
你也可能想要
修改此部分 -
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("User");
String userId = mFirebaseDatabase.push().getKey();
User user = new User(email, password, type);
mAuth.createUserWithEmailAndPassword(email, password);
fbUser = FirebaseAuth.getInstance().getCurrentUser();
mFirebaseDatabase.child(fbUser.getUid()).setValue(user);
mProgressbar.setVisibility(View.GONE);
startActivity(new Intent(RegisterUser.this, HomePage.class));
这个
mAuth.createUserWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
mAuth.signInWithEmailAndPassword(email,password)
fbUser =FirebaseAuth.getInstance().getCurrentUser();
String userId = fbUser.getUid();
mFirebaseDatabase=
FirebaseDatabase.getInstance().getReference("User").child(userId);
mFirebaseDatabase.child("<Key>").setValue("<Value>");
// Go on adding as many user details you want to
mProgressbar.setVisibility(View.GONE);
startActivity(new Intent(RegisterUser.this, HomePage.class));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Do something optional
});
这种方法的优点 - 这样,在检索时你可以简单地使用Child Uid查看用户节点来获取登录用户的详细信息(不用于循环)