默认FirebaseApp未初始化android studio

时间:2020-08-13 10:45:38

标签: java android android-studio

运行应用程序时,启动应用程序时出现错误。它向我表明我忘了使用FireBase做某事,它也向我表明了这一点:java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.t.androiduberremake. Make sure to call FirebaseApp.initializeApp(Context) first.

there's my code

private final  static int LOGIN_REQUEST_CODE =7171;
private List<AuthUI.IdpConfig> providers;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener listener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(this);
         init();

}

private void init() {
    providers = Arrays.asList(
            new AuthUI.IdpConfig.PhoneBuilder().build(),
            new AuthUI.IdpConfig.GoogleBuilder().build());


    firebaseAuth  =FirebaseAuth.getInstance();
     listener  = myFirebaseAuth ->{
         FirebaseUser user = myFirebaseAuth.getCurrentUser();
         if(user != null)
             delaySplashScreen();
         else
             showLoginLayout();
     };
}

private void showLoginLayout() {
    AuthMethodPickerLayout authMethodPickerLayout = new AuthMethodPickerLayout
            .Builder(R.layout.layout_sign_in).setPhoneButtonId(R.id.btn_phone_sign_in)
            .setGoogleButtonId(R.id.btn_google_sign_in).build() ;



    startActivityForResult(AuthUI.getInstance()
            .createSignInIntentBuilder()
            .setAuthMethodPickerLayout(authMethodPickerLayout)
            .setIsSmartLockEnabled(false).setAvailableProviders(providers)
            .build(), LOGIN_REQUEST_CODE);

}

private void delaySplashScreen() {
    Completable.timer(5, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
            .subscribe(() -> Toast.makeText(SplashScreenActivity.this, "Splash Screen done!! ", Toast.LENGTH_SHORT).show());


}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

     if(requestCode == LOGIN_REQUEST_CODE){

         IdpResponse response = IdpResponse.fromResultIntent(data);
         if(resultCode == RESULT_OK){
             FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
         }
         else {
             Toast.makeText(this, "[ERROR]: "+ response.getError().getMessage(), Toast.LENGTH_SHORT).show();
         }
     }
}  

这是我尝试运行给定应用程序时控制台向我显示的内容:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.t.androiduberremake/com.t.androiduberremake.SplashScreenActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.t.androiduberremake. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3760)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8147)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.t.androiduberremake. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
        at com.google.firebase.auth.FirebaseAuth.getInstance(com.google.firebase:firebase-auth@@19.3.2:1)
        at com.t.androiduberremake.SplashScreenActivity.init(SplashScreenActivity.java:49)
        at com.t.androiduberremake.SplashScreenActivity.onCreate(SplashScreenActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:8066)
        at android.app.Activity.performCreate(Activity.java:8054)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3733)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8147) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
2020-08-13 15:33:55.746 12265-12265/? I/Process: Sending signal. PID: 12265 SIG: 9

1 个答案:

答案 0 :(得分:0)

您尚未将google-services插件添加到build.gradle。这一点很重要。 根据{{​​3}},您还必须添加:

apply plugin: 'com.google.gms.google-services'