java - Firebase 身份验证中的登录 Activity 不是从新 Activity 启动的

标签 java android authentication firebase

我正在使用一个新 Activity 来启动 Firebase 身份验证 Activity 并在我的 NodeJS API 中发布经过身份验证的用户,但即使我没有进行身份验证,Firebase 身份验证 Activity 也不会启动。

代码如下:

 

    public class loginActivity extends AppCompatActivity {
        FirebaseAuth auth;
        private static final int RC_SIGN_IN=1;
        double latitude,longitude;
        boolean isAuthDone=false;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            auth=FirebaseAuth.getInstance();
            if(auth.getCurrentUser() != null){
                //user already logged in
                Log.d("TAG     ", auth.getCurrentUser().getEmail()+" already logged in.");
                onBackPressed();
            }
            else {

                    loginFunction();

            }

            String name=auth.getCurrentUser().getDisplayName(); //LINE 54
            String email=auth.getCurrentUser().getEmail();

            // POST TO API
            GPSTracker tracker = new GPSTracker(this);
            if (!tracker.canGetLocation()) {
                tracker.showSettingsAlert();
            } else {
                latitude = tracker.getLatitude();
                longitude = tracker.getLongitude(); //LOCATION
            }


            String loc = latitude+", "+longitude;
            Users newUser=new Users(name, email, loc, "0", false);

    //        if(!isAuthDone){
    //            while(!isAuthDone){
    //                loginFunction();    //ASK FOR LOGIN UNTIL THE USER AUTHENTICATES.
    //            }
    //        }

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiUrls.BASEURL)
                    .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                    .build();
            ApiInterface apiI;
            apiI= retrofit.create(ApiInterface.class);
            Call call;
            call=apiI.saveUser(newUser);
            call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    if(response.isSuccessful()){
                        Log.e("User reg. in API",response.message()+response.code());
                        Users user=response.body();
                        ApiUrls.userName=auth.getCurrentUser().getDisplayName();
                        ApiUrls.userID=user.getID()+"";     //THIS IS VERY IMPORTANT.
                        ApiUrls.userEmail=user.getEmail();
                        ApiUrls.profilePicURL=auth.getCurrentUser().getPhotoUrl();
                        ApiUrls.isUserLoggedInAndSaved=true;
                    }
                }
                @Override
                public void onFailure(Call call, Throwable t) {
                    Log.e("TAG", "Not posted in API");
                    AuthUI.getInstance()
                            .signOut(loginActivity.this)
                            .addOnCompleteListener(new OnCompleteListener() {
                                @Override
                                public void onComplete(Task task) {
                                    Log.d("TAG    ", "Signed out from this app.");
                                    onBackPressed();
                                }
                            });
                    ApiUrls.isUserLoggedInAndSaved=false;
                    //Sign out from the email and launch a DIALOG to try again.
                }
            });
            onBackPressed();

        }

        private void loginFunction() {
                startActivityForResult(AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setProviders(AuthUI.FACEBOOK_PROVIDER,
                                AuthUI.EMAIL_PROVIDER,
                                AuthUI.GOOGLE_PROVIDER).build(),RC_SIGN_IN);

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == RC_SIGN_IN){
                if(resultCode == RESULT_OK){
                    Log.d("TAG logged in", auth.getCurrentUser().getEmail());
                    isAuthDone=true;
                }
                else{
                    Log.d("TAG    ", "USER NOT AUTHENTICATED");
                    //Launch a dialog to turn the internet ON and redirect to some other activity.
                }
            }
        }
    }

 

早些时候我从很多地方启动 Firebase 身份验证 Activity 。它在那里工作,但现在它不工作了。错误是:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getDisplayName()' on a null object reference at com.example.aavgeensingh.shuddhi.Activities.loginActivity.onCreate(loginActivity.java:54)

编辑:我现在知道原因了。但是有什么方法可以让我先等待用户进行身份验证,然后使用改造将详细信息发布到我的 API。

最佳答案

调用 loginFunction() 后,onCreate() 函数的其余部分继续执行,因此用户可以为空。您应该在此之前结束执行。

关于java - Firebase 身份验证中的登录 Activity 不是从新 Activity 启动的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44608919/

相关文章:

java - JAXB:默认情况下哪些元素是可编码|不可编码的?

java - 第二次尝试打开日历应用程序崩溃

authentication - 无需 SSL 的安全身份验证

android - "ImportError: No module named readline"运行 "repo init"

javascript - 在 nodejs Passport.js 中禁用 session

java - 如何防止出现 "Authentication Required"框?

java - 无法在java中的tcpserver和tcpclient之间连续发送数据

java - 在 Eclipse 中生成可运行 JAR 的热键

JAVA - Conway 的生命游戏删除所有活细胞?

java - 如何使用切换按钮停止方法?