我是Firebase的新手,我创建了我的第一个应用程序。
我有2个问题,我想得到一些帮助。
我已将Android Studio应用程序链接到Firebase项目,并为新用户的Sigup创建了代码。
SignupActivity.java
package com.company.fbaseapp.activities;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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.company.fbaseapp.R;
public class SignupActivity extends AppCompatActivity {
private static final String TAG = "SignupActivity";
private EditText mUsername;
private EditText mEmail;
private EditText mPassword;
private Button mSignup;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
//mUsername = (EditText) findViewById(R.id.input_username);
mEmail = (EditText) findViewById(R.id.input_email);
mPassword = (EditText) findViewById(R.id.input_password);
mSignup = (Button) findViewById(R.id.btn_signup);
mSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: attempting to signup.");
// Check for null valued EditText fields
if (!isEmpty (mEmail.getText().toString()) && !isEmpty (mPassword.getText().toString())) {
signupNewUser(mEmail.getText().toString(), mPassword.getText().toString());
//Toast.makeText(SignupActivity.this, "Thank you for signing up", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(SignupActivity.this, "You must fill out all the fields", Toast.LENGTH_SHORT).show();
}
}
} );
hideSoftKeyboard();
}
private void signupNewUser(String email, String password) {
showDialog();
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener( new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "onComplete: onComplete: " + task.isSuccessful());
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: AuthState: " + FirebaseAuth.getInstance()
.getCurrentUser()
.getUid());
FirebaseUser currentperson=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(currentperson.getUid());
ref.child("Username").setValue(name);
FirebaseAuth.getInstance().signOut();
redirectLoginScreen();
}
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Unable to signup", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Return true if the @param is null
* @param string
* @return
*/
private boolean isEmpty(String string){
return string.equals("");
}
/**
* Redirects the user to the login screen
*/
private void redirectLoginScreen(){
Log.d(TAG, "redirectLoginScreen: redirecting to login screen.");
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void showDialog(){
mProgressBar.setVisibility(View.VISIBLE);
}
private void hideDialog(){
if(mProgressBar.getVisibility() == View.VISIBLE){
mProgressBar.setVisibility(View.INVISIBLE);
}
}
private void hideSoftKeyboard(){
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
logcat的
12-30 01:19:18.492 31917-31917/com.company.fbaseapp D/SignupActivity: onClick: attempting to signup.
12-30 01:19:18.493 31917-31917/com.company.fbaseapp D/AndroidRuntime: Shutting down VM
12-30 01:19:18.497 31917-31917/com.company.fbaseapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.fbaseapp, PID: 31917
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.company.fbaseapp.activities.SignupActivity.showDialog(SignupActivity.java:126)
at com.company.fbaseapp.activities.SignupActivity.signupNewUser(SignupActivity.java:72)
at com.company.fbaseapp.activities.SignupActivity.access$300(SignupActivity.java:21)
at com.company.fbaseapp.activities.SignupActivity$1.onClick(SignupActivity.java:54)
at android.view.View.performClick(View.java:6289)
at android.view.View$PerformClick.run(View.java:24800)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
12-30 01:19:18.499 31917-31917/com.company.fbaseapp D/AppTracker: App Event: crash
第一个问题是当我点击 mSignup 按钮时应用程序崩溃。
第二个问题是,我想知道如何为每个注册用户添加唯一用户名。
请帮我弄清楚如何解决这两个问题。
由于
答案 0 :(得分:1)
这是错误:
`Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference`
进度条为空,您需要添加:
mProgressBar = (ProgressBar) findViewById(R.id.progressBar); //your id in xml
关于每个用户的唯一用户名,您可以这样做:
FirebaseUser currentperson=FirebaseAuth.getInstance().getCurrentUser();
然后,如果要保存到数据库,则可以执行以下操作:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users");
ref.child(currentperson.getUid());
以上内容将为每个用户名添加唯一的用户ID。
如果您要在身份验证中添加唯一的用户名,则无法执行此操作。但默认情况下,该电子邮件是唯一的,因此如果您尝试使用相同的电子邮件注册其他用户,那么您将获得error:authentication failed
编辑:
您无法在身份验证中添加username
,如果您转到控制台,则会看到添加了userid
和email
。身份验证为您提供userid
但无法在身份验证中添加用户名,只能在数据库中添加,可以执行此操作:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(currentperson.getUid());
ref.child("Username").setValue(name);
然后在你的firebase数据库中你会得到这个:
Users
LA0rRvP4TrewYbMSl0bDA3ork8h2
Username: hisname_here