1
votes

I am relatively new to Android Studio and Firebase as well. I have done the setup and connected to Firebase, but this error keeps popping out, causing the app to crash:

 --------- 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) 

I have also Initialize Firebase Auth like so, mAuth = FirebaseAuth.getInstance(); but I still get the same error

I also did included the dependencies classpath 'com.google.gms:google- services:4.1.0'

and plugins apply plugin: 'com.google.gms.google-services' which are mentioned in most solutions, but nothing seems to work

This is the following code and according to the logcat, the error in line 48 points to 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
Thanks guys! Already found the solution! Thank you all, appreciate it!NewUser

4 Answers

1
votes

Make sure you follow every step of the firebase docs:
Firebase documentation on auth
You need to add google services to your project level gradle file:

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

And you need to add to your app level gradle file the firebase core dependency:

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

Make sure you enabled the auth options in the console under sign in methods

0
votes

Your logcat itself gives the solution to your problem:

You need to call FirebaseApp.initializeApp(Context) in your application class.

0
votes

You are getting this error because in your code, you have not initialized the firebase.

just add this in your on create method of the activity

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

As pointed by someone earlier too, the error indicates that you've either missed invoking FirebaseApp.initializeApp(Context) method or the sequence is incorrect.

There has been recent updates in using Firebase Auth SDK and the new approach is quite simple and minimal code. Please refer here for more details https://firebase.google.com/docs/auth/android/firebaseui

This is the approach to setup Auth providers and launch auth intent. You would need to add separate logic for Callaback handling and UI customization (if applicable) that too is available on above mentioned docs link.

// 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);