How can I solve the Android Firebase error “Defaul

2020-05-27 08:32发布

I am using FirebaseAuth for user registration with email and password, and I have already added the plugin and dependencies in my project.

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText ed_email, ed_pass;
Button but_login;
ProgressDialog progressDialog;
FirebaseAuth firebaseAuth;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=getApplicationContext();
    FirebaseApp.initializeApp(context);
    firebaseAuth=FirebaseAuth.getInstance();

    ed_email= (EditText) findViewById(R.id.ed_email);
    ed_pass= (EditText) findViewById(R.id.ed_pass);
    but_login= (Button) findViewById(R.id.but_login);
    but_login.setOnClickListener(this);
    progressDialog=new ProgressDialog(this);
}
public void registerUser(){
    String email=ed_email.getText().toString().trim();
    String pass=ed_pass.getText().toString().trim();
    if(TextUtils.isEmpty(email)){
        Toast.makeText(getApplicationContext(),"Invalid Input",Toast.LENGTH_SHORT).show();
        return;
    }
    if(TextUtils.isEmpty(pass)){
        Toast.makeText(getApplicationContext(),"Invalid Input",Toast.LENGTH_SHORT).show();
        return;
    }
    progressDialog.setMessage("You are registering...");
    progressDialog.show();
    firebaseAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                progressDialog.hide();
            } else {
                Toast.makeText(getApplicationContext(), "Sorry...!!!", Toast.LENGTH_SHORT).show();
                progressDialog.hide();
            }
        }

    });


}

@Override
public void onClick(View v) {
    registerUser();
}

}

logcat -

com.skapsdevelopment.firebase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skapsdevelopment.firebase, PID: 31993
Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, iconPack:com.baranovgroup.nstyle, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skapsdevelopment.firebase/com.skapsdevelopment.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.skapsdevelopment.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5461)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.skapsdevelopment.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
   at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
   at com.skapsdevelopment.firebase.MainActivity.onCreate(MainActivity.java:35)
   at android.app.Activity.performCreate(Activity.java:6251)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5461) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

My App is not starting and showing the following error:

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this 
process com.skapsdevelopment.firebase. 
Make sure to call FirebaseApp.initializeApp(Context) first.

Why is the app not starting properly?

12条回答
叛逆
2楼-- · 2020-05-27 09:04

please add this classpath 'com.google.gms:google-services:4.0.1' at the place of classpath 'com.google.gms:google-services:4.1.0'

查看更多
劫难
3楼-- · 2020-05-27 09:11

Double-check the following as this has worked for me:

Added in your project level gradle file:

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

And on your app-build gradle file:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:10.0.1'

  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services' 

Link

查看更多
Viruses.
4楼-- · 2020-05-27 09:11

Downgrade google service plugin from version 4.1.0 to version 4.0.1 in project's build.gradle

classpath 'com.google.gms:google-services:4.0.1'
查看更多
神经病院院长
5楼-- · 2020-05-27 09:13

Bit late for the party, but I wanted to share my solution. Hopefully it helps others

I had a very similar problem, a few hours ago. Tried everything here, but nothing worked. All my dependencies were correct, which made no sense.

What I didn't know was that this application needed to be registered in Firebase. I followed this step by step guide and voilà! It works! Instead of using the recommended classpath dependency, be sure to use this one com.google.gms:google-services:4.2.0.

查看更多
We Are One
6楼-- · 2020-05-27 09:17

In my case Proguard was causing the problem so this is what I did: - If there is any class in the AndroidManifest.xml with "YourClass" or ".YourClass", change it to its full package name: "com.yourpackage.yourClass"

查看更多
戒情不戒烟
7楼-- · 2020-05-27 09:18

It's stated in the Docs that the FirebaseApp.initializeApp() should be called from the Application.

Did you try to create and Application instance (if you don't have one already), and call FirebaseApp.initializeApp(this) from its onCreate() method ?

查看更多
登录 后发表回答