使用电子邮件和密码进行Firebase身份验证

时间:2019-01-17 06:58:07

标签: android firebase firebase-authentication

我对Android Studio和Firebase还是比较陌生。我已经完成设置并连接到Firebase,但是此错误不断弹出,导致应用崩溃:

 --------- beginning of crash
2019-01-17 14:38:11.420 10795-10795/com.example.asus.cab E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.asus.cab, PID: 10795
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.cab/com.example.asus.cab.DriverLoginRegisterActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.asus.cab. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6642)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.asus.cab. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.4:240)
        at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
        at com.example.asus.cab.DriverLoginRegisterActivity.onCreate(DriverLoginRegisterActivity.java:45)
        at android.app.Activity.performCreate(Activity.java:7131)
        at android.app.Activity.performCreate(Activity.java:7122)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2882)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6642) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
  

我也像这样初始化Firebase身份验证,mAuth = FirebaseAuth.getInstance();但我仍然遇到相同的错误

     

我还包括了依赖项classpath 'com.google.gms:google- services:4.1.0'

     

和插件apply plugin: 'com.google.gms.google-services'   大多数解决方案中都提到了这些,但是似乎没有任何作用

     

这是下面的代码,根据日志记录,第48行中的错误指向mAuth = FirebaseAuth.getInstance();

public class DriverLoginRegisterActivity extends AppCompatActivity {

    private Button DriverLoginButton;
    private Button DriverRegisterButton;
    private TextView DriverRegisterLink;
    private TextView DriverStatus;
    private EditText EmailDriver;
    private EditText PasswordDriver;
    private ProgressDialog loadingBar;
    private FirebaseAuth mAuth;

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

        DriverLoginButton = (Button) findViewById(R.id.driver_login_btn);
        DriverRegisterButton = (Button) findViewById(R.id.driver_register_btn);
        DriverRegisterLink = (TextView) findViewById(R.id.register_driver_link);
        DriverStatus = (TextView) findViewById(R.id.driver_status);
        EmailDriver = (EditText) findViewById(R.id.email_driver);
        PasswordDriver = (EditText) findViewById(R.id.password_driver);
        loadingBar = new ProgressDialog(this);

        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();

        DriverRegisterButton.setVisibility(View.INVISIBLE);
        DriverRegisterButton.setEnabled(false);

        DriverRegisterLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DriverLoginButton.setVisibility(View.INVISIBLE);
                DriverRegisterLink.setVisibility(View.INVISIBLE);
                DriverStatus.setText("Register Driver");   
                DriverRegisterButton.setVisibility(View.VISIBLE);
                DriverLoginButton.setEnabled(true);
            }
        });

        DriverRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                String email = EmailDriver.getText().toString();
                String password = PasswordDriver.getText().toString();

                RegisterDriver(email, password);
            }
        });


    }

    private void RegisterDriver(String email, String password) {
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(DriverLoginRegisterActivity.this, "Please Enter Email..", Toast.LENGTH_SHORT).show();

        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(DriverLoginRegisterActivity.this, "Please Enter Password..", Toast.LENGTH_SHORT).show();

        } else {
            loadingBar.setTitle("Driver Registration");
            loadingBar.setMessage("Please wait...");
            loadingBar.show();


            mAuth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(DriverLoginRegisterActivity.this, "Driver Register Succesful..", Toast.LENGTH_SHORT).show();
                                loadingBar.dismiss();
                            } else {
                                Toast.makeText(DriverLoginRegisterActivity.this, "Registration Unsuccessful, Please Try Again..", Toast.LENGTH_SHORT).show();
                                loadingBar.dismiss();
                            }
                        }
                    });
        }
    }
}

4 个答案:

答案 0 :(得分:1)

确保您遵循firebase文档的每个步骤:
Firebase documentation on auth
您需要将Google服务添加到您的项目级Gradle文件:

classpath 'com.google.gms:google-services:4.2.0'

您需要将firebase核心依赖项添加到您的应用程序级别gradle文件中:

implementation 'com.google.firebase:firebase-core:16.0.6'

确保在控制台的登录方法下启用了auth选项

答案 1 :(得分:0)

您的logcat本身可以解决您的问题:

您需要在应用程序类中调用 FirebaseApp.initializeApp(Context)

答案 2 :(得分:0)

您收到此错误是因为在您的代码中,您尚未初始化Firebase。

只需将其添加到活动的on create方法中

FirebaseApp.initializeApp(this);
mAuth = FirebaseAuth.getInstance();

答案 3 :(得分:0)

也正如先前的人所指出的那样,该错误表明您错过了调用FirebaseApp.initializeApp(Context)方法或顺序不正确。

使用Firebase Auth SDK已有最新更新,新方法非常简单且代码最少。有关更多详细信息,请参见此处https://firebase.google.com/docs/auth/android/firebaseui

这是设置身份验证提供程序和启动身份验证意图的方法。您需要为Callaback处理和UI自定义添加单独的逻辑(如果适用),上述逻辑也可以在上述docs链接上获得。

// Choose authentication providers
List<AuthUI.IdpConfig> providers = Arrays.asList(
        new AuthUI.IdpConfig.EmailBuilder().build(),
        new AuthUI.IdpConfig.PhoneBuilder().build(),
        new AuthUI.IdpConfig.GoogleBuilder().build(),
        new AuthUI.IdpConfig.FacebookBuilder().build(),
        new AuthUI.IdpConfig.TwitterBuilder().build());

// Create and launch sign-in intent
startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .build(),
        RC_SIGN_IN);