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 workThis 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();
}
}
});
}
}
}
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:
And you need to add to your app level gradle file the firebase core dependency:
Make sure you enabled the auth options in the console under sign in methods