我正在创建一个客户注册页面,该页面将客户添加到我的Firebase数据库中。一旦注册,该应用程序应移至下一个活动,即Google地图。客户被添加到数据库中,但是在进行下一个活动时崩溃。这是我的代码:
SignupActivityCustomer.java:
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.util.Patterns;
import android.view.View;
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.google.firebase.database.FirebaseDatabase;
public class SignupActivityCustomer extends AppCompatActivity implements View.OnClickListener {
private EditText editTextName, editTextEmail, editTextPassword, editTextPhone;
private ProgressBar progressBar;
private FirebaseAuth mAuth;
private static final String TAG = "SignupActivityCustomer";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_customer);
editTextName = findViewById(R.id.edit_text_name);
editTextEmail = findViewById(R.id.edit_text_email);
editTextPassword = findViewById(R.id.edit_text_password);
editTextPhone = findViewById(R.id.edit_text_phone);
progressBar = findViewById(R.id.progressbar);
progressBar.setVisibility(View.GONE);
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.button_register).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() != null) {
//handle the already login user
}
}
private void registerUser() {
final String name = editTextName.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
final String phone = editTextPhone.getText().toString().trim();
if (name.isEmpty()) {
editTextName.setError(getString(R.string.input_error_name));
editTextName.requestFocus();
return;
}
if (email.isEmpty()) {
editTextEmail.setError(getString(R.string.input_error_email));
editTextEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editTextEmail.setError(getString(R.string.input_error_email_invalid));
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()) {
editTextPassword.setError(getString(R.string.input_error_password));
editTextPassword.requestFocus();
return;
}
if (password.length() < 6) {
editTextPassword.setError(getString(R.string.input_error_password_length));
editTextPassword.requestFocus();
return;
}
if (phone.isEmpty()) {
editTextPhone.setError(getString(R.string.input_error_phone));
editTextPhone.requestFocus();
return;
}
if (phone.length() != 10) {
editTextPhone.setError(getString(R.string.input_error_phone_invalid));
editTextPhone.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "adding customer");
if (task.isSuccessful()) {
Customer user = new Customer(
name,
email,
phone
);
FirebaseDatabase.getInstance().getReference("Customers")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
Log.d(TAG, "customer added");
if (task.isSuccessful()) {
startActivity(new Intent(SignupActivityCustomer.this, CustomerHome.class));
} else {
//display a failure message
}
}
});
} else {
Toast.makeText(SignupActivityCustomer.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_register:
registerUser();
break;
}
}
}
Logcat error:
2019-01-26 11:29:10.843 4038-4038/com.example.julie.fyp D/SignupActivityCustomer: customer added
2019-01-26 11:29:11.669 1880-1959/? W/InputDispatcher: channel '4a251a2 com.example.julie.fyp/com.example.julie.fyp.SignupActivityCustomer (server)' ~ Consumer closed input channel or an error occurred. events=0x9
2019-01-26 11:29:11.669 1880-1959/? E/InputDispatcher: channel '4a251a2 com.example.julie.fyp/com.example.julie.fyp.SignupActivityCustomer (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-01-26 11:29:11.719 1880-2523/? W/InputDispatcher: Attempted to unregister already unregistered input channel '4a251a2 com.example.julie.fyp/com.example.julie.fyp.SignupActivityCustomer (server)'
答案 0 :(得分:0)
据我所知,Firebase现在可以异步工作,因此它正在另一个线程上工作。
您可以尝试将startActivity
放入runOnUiThread(Runnable)
代码中
if (task.isSuccessful()) {
runOnUiThread(new Runnable(){
public void run() {
startActivity(new Intent(SignupActivityCustomer.this, CustomerHome.class));
}
});
} else {
//display a failure message
}
希望它能起作用