我一直在努力找出代码中出了什么问题。我有一个Firebase应用程序,该应用程序在通过身份验证后会进入导航抽屉,但会加载到空白屏幕。仅测试setupUI()函数,导航抽屉即可工作,但是在嵌套函数中没有任何错误,因此我对正在发生的事情感到非常困惑。我的onActivityResult是给我带来问题的原因。谁能帮我吗?感谢您的指导。如果您需要代码的其他任何部分,请告诉我。
我的主要活动代码如下:
public class MainActivity extends AppCompatActivity {
DatabaseReference user_information;
private static final int MY_REQUEST_CODE = 7117;
List<AuthUI.IdpConfig> providers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Paper.init(this);
// Init firebase
user_information = FirebaseDatabase.getInstance().getReference(Common.USER_INFORMATION);
// init providers
providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()
);
// Request Permission Locatoin
Dexter.withActivity(this)
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
showSignInOptions();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(MainActivity.this, "you gotta accept the terms", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
}
}).check();
}
private void showSignInOptions() {
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(providers).build(),MY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setupUI();
if(requestCode == MY_REQUEST_CODE){
IdpResponse response = IdpResponse.fromResultIntent(data);
if(resultCode == RESULT_OK)
{
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
//Check if user exists on DB
user_information.orderByKey()
.equalTo(firebaseUser.getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue() == null){
if(!dataSnapshot.child(firebaseUser.getUid()).exists()){
Common.loggedUser = new User(firebaseUser.getUid(),firebaseUser.getEmail());
// Add to database
user_information.child(Common.loggedUser.getUid())
.setValue(Common.loggedUser);
}
}
else {
Common.loggedUser = dataSnapshot.child(firebaseUser.getUid()).getValue(User.class);
}
Paper.book().write(Common.USER_UID_SAVE_KEYS,Common.loggedUser.getUid());
updateToken(firebaseUser);
setupUI();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
}
private void setupUI() {
startActivity(new Intent(MainActivity.this,HomeActivity.class));
finish();
}
private void updateToken(final FirebaseUser firebaseUser) {
final DatabaseReference tokens = FirebaseDatabase.getInstance()
.getReference(Common.TOKENS);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
tokens.child(firebaseUser.getUid())
.setValue(instanceIdResult.getToken());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
我的普通班级文件是:
public class Common {
public static final String USER_INFORMATION = "UserInformation";
public static final String USER_UID_SAVE_KEYS = "SaveUid" ;
public static final String TOKENS = "Tokens";
public static User loggedUser;
}