因此,我是android studio和firebase的新手,我正在做一个android应用程序,需要在该应用程序中创建用户角色,以查看应用程序中的不同功能(客户端和培训师)。我知道通过Firebase身份验证无法使用此功能,因此建议我将用户详细信息插入数据库中,然后从那里分配/访问角色。我正在尝试使数据库看起来像这样……
How i'm trying to structure the real-time database (manually added to database)
//其中“ User1Email”可以是用户或用户ID的电子邮件(因为我认为您不能在数据库中包含@)
但是我的代码似乎使应用程序崩溃了。我尝试将数据更新到.java文件中的数据库的方式看起来像这样。
//ESTABLISH THE CONNECTIONS TO DATABASE AND GET TE USER ID
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference mRef = database.getReference("users");
EditText emailField = findViewById(R.id.email);
EditText passwordField = findViewById(R.id.password);
//Get email and password from text views
final String email = emailField.getText().toString();
final String password = passwordField.getText().toString();
mRef.child("password").setValue(password);
mRef.child("role").setValue("Trainer");
// I DID ALSO TRY THIS BUT UID STAYS UNDERLINED RED- mRef.child(uid).child("password").setValue(password);
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.finalyearapp, PID: 13903
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.finalyearapp.SignUpActivity.onClick(SignUpActivity.java:53)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:3)
在获取用户实例之前,您必须先对其进行身份验证。
要确保在执行任何操作之前已登录用户,请在中调用getCurrentUser()
方法
createUserWithEmailAndPassword()
(如果您使用的是电子邮件和密码身份验证)。
首先创建并初始化Firebase Auth对象:
FirebaseAuth mAuth = FirebaseAuth.getInstance();
然后,当您要注册(带有按钮)时,调用createUserWithEmailAndPassword()
方法:
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//Successfully created user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference mRef = database.getReference("users");
EditText emailField = findViewById(R.id.email);
//Get email from EditText
final String email = emailField.getText().toString();
mRef.child("role").setValue("Trainer");
} else {
//User not created,
//Print exception
Log.e("Sign up", task.getException().getMessage())
//Write your code to handle user creation fail
}
}
});
如果您想了解有关Firebase身份验证的更多信息,请查看here
请记住,此解决方案仅用于创建用户。
希望有帮助!写评论以得到更多解释,或者如果您有其他问题!
答案 1 :(得分:0)
在数据库中,您有Users
个节点,其节点的大小写为U
,因此应更改以下内容:
final DatabaseReference mRef = database.getReference("users");
对此:
final DatabaseReference mRef = database.getReference("Users");
另外,您需要添加child()
来包含userId
:
final DatabaseReference mRef = database.getReference("Users").child(uid);
mRef.child("password").setValue(password);
mRef.child("role").setValue("Trainer");
那么您将拥有:
Users
userId
password : password
role : Trainer