gpt4 book ai didi

java - 使用 Firebase 身份验证 UID 作为实时数据库中的子节点

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:48 26 4
gpt4 key购买 nike

我尝试使用用户的 auth UID 作为子节点将用户添加到 Firebase 中的实时数据库中。

我一直使用这个问题作为指导:

https://stackoverflow.com/questions/40000899/how-to-link-between-authenticated-users-and-database-in-firebase

当我在 Debug模式下运行项目时,会添加用户。但是,当我正常运行该应用程序时,它会崩溃。我收到的错误是空指针异常。

这是我的代码:

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)

任何帮助将不胜感激。

最佳答案

通过使用 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);
}
}

关于java - 使用 Firebase 身份验证 UID 作为实时数据库中的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48326703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com