更新
问题是我无法在EditText上显示用户电子邮件
我试图将onStart和onStop mauth监听器放入,然后当我运行应用程序并输入此活动时,用户看到为null并将我踢回登录活动。这有什么解决方案吗?
public class ProfileActivity extends AppCompatActivity {
private EditText newEmail;
private FirebaseAuth.AuthStateListener mauthListener;
private FirebaseAuth mauth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//get current user
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
//get firebase auth instance
mauth = FirebaseAuth.getInstance();
newEmail = (EditText) findViewById(R.id.email1);
//tracking the sign in and singn out operations
mauthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
// user auth state is changed - user is null
// launch login activity
startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
finish();
}
}
};
setDataToView();
}
private void setDataToView() {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null){
newEmail.setText("" + user.getEmail());
} }
已更新 我不能在EditText上显示用户电子邮件,现在有什么不对吗?
答案 0 :(得分:0)
因此, setDataToView()中的firebase用户为null,因为您将usermail设置为Edittext而不检查是否为null以修复您有两种可能的方法。
一个是这样做的:
mauthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
// user auth state is changed - user is null
// launch login activity
startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
finish();
}else{
newEmail.setHint("" + user.getEmail());
}
}
};
其他方式就是这样做:
private void setDataToView() {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null){
newEmail.setHint("" + user.getEmail());
}
}