默认FirebaseApp未初始化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

您还没有将谷歌服务插件添加到您的build.gradle中。这很重要。根据文档,您还必须添加:

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

相关内容

最新更新