我在阅读官方文档后问这个问题。 我设法在我的Firebase身份验证系统上注册用户,但我想在注册成功后启动一个活动
这是我的代码
public class SignUpActivity extends AppCompatActivity {
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
mAuth=FirebaseAuth.getInstance();
}
public void fgh(View view) {
mAuth.createUserWithEmailAndPassword("example@email.com","password")
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(SignUpActivity.this, "Successfully created your account", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,SuccessActivity.class); //this is my error
startActivity(intent); // how to start activity here
}
else{
Toast.makeText(SignUpActivity.this, "ERROR has occurred", Toast.LENGTH_LONG).show();
}
}
});
}
}
答案 0 :(得分:2)
获取正确的背景,让它成为
Intent intent = new Intent(SignUpActivity.this,SuccessActivity.class);
答案 1 :(得分:0)
要解决此问题,请使用如下方法调用更改您传递的this
作为第一个参数:
Intent intent = new Intent(getApplicationContext(), SuccessActivity.class);
您可以使用另一种方法声明全局变量:
private Context context;
并在onCreate()
方法中将其初始化为:
context = this;
然后在您的活动中随处使用它。