I am trying to signup using firebase with email and password but when I am adding onclicklistner on signup button and try to signup and run app then Gradle build is successfully run but the app is crashing there is no error in the code here the code of signup activity:
public class SignUpActivity extends AppCompatActivity {
ActivitySignUpBinding binding;
private FirebaseAuth mAuth;
FirebaseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySignUpBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().setTitle("Sign Up");
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
database= FirebaseDatabase.getInstance();
binding.SignUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.createUserWithEmailAndPassword(binding.EmailTv.getText().toString() ,binding.PasswordTv.
getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "User created successfully",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(SignUpActivity.this, task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
binding.alreadyHaveAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(SignUpActivity.this, SignInActivity.class);
startActivity(intent);
}
});
}
}
Gradle build app level:
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android { compileSdk 30
defaultConfig {
applicationId "com.example.myapplication"
minSdk 16
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//implementation 'com.google.firebase:firebase-auth:21.0.1'
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.3.0')
// Declare the dependency for the Firebase Authentication library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-database'
}
Logcat code:
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:183)
at com.google.firebase.auth.FirebaseAuth.getInstance(com.google.firebase:firebase-auth@@21.0.1:1)
at com.example.myapplication.SignUpActivity.onCreate(SignUpActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
.when I remove onclicklistner from the signup button the app launch. please help me to solve this problem.