在多个设备上使用登录身份验证 - Android App

时间:2018-02-15 21:51:39

标签: java android firebase nullpointerexception firebase-authentication

我在使用我的应用程序时遇到困难。

我正在使用Firebase提供用户身份验证,该功能正常,直到我在其他手机/设备上试用。

当我输入电子邮件和密码并按“登录”时,应用程序崩溃,日志显示以下内容:

FATAL EXCEPTION: main Process: com.example.android.frapp, PID: 14822
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)' on a null object reference
                                                                           at com.google.android.gms.internal.zzdtp.zzb(Unknown Source)
                                                                           at com.google.android.gms.internal.zzdtw.zzb(Unknown Source)
                                                                           at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
                                                                           at com.example.android.frapp.MainLoginActivity.startSiginIn(MainLoginActivity.java:105)
                                                                           at com.example.android.frapp.MainLoginActivity.access$000(MainLoginActivity.java:23)
                                                                           at com.example.android.frapp.MainLoginActivity$2.onClick(MainLoginActivity.java:65)
                                                                           at android.view.View.performClick(View.java:5197)
                                                                           at android.view.View$PerformClick.run(View.java:20926)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:145)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5951)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

这一切都发生在我的主要登录类(我在下面提供)中:

public class MainLoginActivity extends AppCompatActivity {

private EditText mEmailField;
private EditText mPasswordField;

private Button mLoginBtn;

private FirebaseAuth mAuth;

private FirebaseAuth.AuthStateListener mAuthListener;

private boolean isUserClickedBackButton = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_login);

    mAuth = FirebaseAuth.getInstance();

    mEmailField = (EditText) findViewById(R.id.emailField);
    mPasswordField = (EditText) findViewById(R.id.passwordField);

    mLoginBtn = (Button) findViewById(R.id.loginBtn);

    // connection to user authentication on firebase (database)
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            if (firebaseAuth.getCurrentUser() != null){

                startActivity(new Intent(MainLoginActivity.this, MainActivity.class));
            }

        }
    };

    mLoginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startSiginIn();


        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

@Override
// code for exiting from app by using back button on login page
public void onBackPressed() {
    //moveTaskToBack(true);
    if (!isUserClickedBackButton){
        Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
        isUserClickedBackButton = true;
    } else {
        System.exit(0); // exits right out of app
        super.onBackPressed();

    }
}


// for login
private void startSiginIn() {

    String email = mEmailField.getText().toString();
    String password = mPasswordField.getText().toString();

    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

        Toast.makeText(MainLoginActivity.this, "Please input email and password", Toast.LENGTH_LONG).show();

    } else {
        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (!task.isSuccessful())

                    Toast.makeText(MainLoginActivity.this, "Sign in problem.  Please check email" +
                            " and password", Toast.LENGTH_LONG).show();
            }
        });
    }
}
}

正如我所说,它在我的主设备上运行良好,但在我安装并尝试在其他设备上登录时却没有。它在模拟器上也可以正常工作。

我已尝试搜索类似的问题,但找不到任何问题。

有没有人有任何想法?

由于

1 个答案:

答案 0 :(得分:1)

如果您的身份验证过程在您的手机/模拟器上正常运行,但在其他设备上进行测试时出现此错误,则表示该设备很可能未安装Google Play Services。确保安装正确,然后重试。