我是编程的新手,正在尝试通过以下教程学习。我正在关注一个教程,遇到了一个问题,不知道要解决它。 在我的MainActivity中,我尝试通过电子邮件和密码对用户进行身份验证。 (工作良好)。之后,HomenActivity应该开始。虽然如此,但由于某种原因它无法启动。不幸的是,我无法理解为什么。如果您能为我指明正确的方向,那将是无比感激。
谢谢大家。
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import com.raindevteam.aservicetracking.Model.User;
import com.raindevteam.aservicetracking.Utils.Common;
import java.util.Arrays;
import java.util.List;
import io.paperdb.Paper;
import static java.sql.DriverManager.println;
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);
setContentView(R.layout.activity_main);
Paper.init(this);
user_information = FirebaseDatabase.getInstance().getReference(Common.USER_INFORMATION);
providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()
);
Dexter.withActivity(this)
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
showSignInOption();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(MainActivity.this, "You need to Accept permission",Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
}
}).check();
}
private void showSignInOption(){
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);
if(requestCode == MY_REQUEST_CODE){
IdpResponse response = IdpResponse.fromResultIntent(data);
if(requestCode == RESULT_OK){
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
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);
}
}
//if user exists
else{
Common.loggedUser = dataSnapshot.child(firebaseUser.getUid()).getValue(User.class);
}
Paper.book().write(Common.USER_UID_SAVE_KEY, 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,"Error"+e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
没有显示错误消息。
答案 0 :(得分:0)
我认为您的setupUI()没有运行,请尝试打印Log.e。检查它是否正常工作。
答案 1 :(得分:0)
您会尝试吗?
private void setupUI(){
startActivity(new Intent(this, HomeActivity.class));
finish();
}