我尝试使用Firebase
创建身份验证活动,如下所示:
private TextView mStatusTextView;
private TextView mDetailTextView;
private EditText mEmailField;
private EditText mPasswordField;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authentication);
// Views
mStatusTextView = (TextView) findViewById(R.id.status);
mDetailTextView = (TextView) findViewById(R.id.detail);
mEmailField = (EditText) findViewById(R.id.field_email);
mPasswordField = (EditText) findViewById(R.id.field_password);
// Buttons
findViewById(R.id.email_sign_in_button).setOnClickListener(this);
findViewById(R.id.email_create_account_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
updateUI(user);
}
};
}
但是我收到以下错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {trakr.Authentication}:java.lang.IllegalStateException:默认FirebaseApp未在此进程trakr.kade_c.trakr中初始化。请务必先调用FirebaseApp.initializeApp(Context)。
即使我添加FirebaseApp.initializeApp(this)
,也会抛出相同的异常。
请注意,我的MainActivity
只是简单地调用我的Authentication
课程:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, Authentication.class));
}