默认的FirebaseApp未初始化,Android Studio。

huangapple go评论103阅读模式
英文:

Default FirebaseApp is not initialized android studio

问题

当我运行应用程序时,启动应用程序时出现错误。它告诉我忘记在Firebase上做某些事情,并显示以下内容:java.lang.IllegalStateException: 在此进程com.t.androiduberremake中未初始化默认的FirebaseApp。请确保首先调用FirebaseApp.initializeApp(Context)

以下是您的代码:

  1. private final static int LOGIN_REQUEST_CODE = 7171;
  2. private List<AuthUI.IdpConfig> providers;
  3. private FirebaseAuth firebaseAuth;
  4. private FirebaseAuth.AuthStateListener listener;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. FirebaseApp.initializeApp(this);
  9. init();
  10. }
  11. private void init() {
  12. providers = Arrays.asList(
  13. new AuthUI.IdpConfig.PhoneBuilder().build(),
  14. new AuthUI.IdpConfig.GoogleBuilder().build());
  15. firebaseAuth = FirebaseAuth.getInstance();
  16. listener = myFirebaseAuth -> {
  17. FirebaseUser user = myFirebaseAuth.getCurrentUser();
  18. if (user != null)
  19. delaySplashScreen();
  20. else
  21. showLoginLayout();
  22. };
  23. }
  24. private void showLoginLayout() {
  25. AuthMethodPickerLayout authMethodPickerLayout = new AuthMethodPickerLayout
  26. .Builder(R.layout.layout_sign_in).setPhoneButtonId(R.id.btn_phone_sign_in)
  27. .setGoogleButtonId(R.id.btn_google_sign_in).build();
  28. startActivityForResult(AuthUI.getInstance()
  29. .createSignInIntentBuilder()
  30. .setAuthMethodPickerLayout(authMethodPickerLayout)
  31. .setIsSmartLockEnabled(false).setAvailableProviders(providers)
  32. .build(), LOGIN_REQUEST_CODE);
  33. }
  34. private void delaySplashScreen() {
  35. Completable.timer(5, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
  36. .subscribe(() -> Toast.makeText(SplashScreenActivity.this, "Splash Screen done!! ", Toast.LENGTH_SHORT).show());
  37. }
  38. @Override
  39. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  40. super.onActivityResult(requestCode, resultCode, data);
  41. if (requestCode == LOGIN_REQUEST_CODE) {
  42. IdpResponse response = IdpResponse.fromResultIntent(data);
  43. if (resultCode == RESULT_OK) {
  44. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  45. } else {
  46. Toast.makeText(this, "[ERROR]: " + response.getError().getMessage(), Toast.LENGTH_SHORT).show();
  47. }
  48. }
  49. }

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

  1. java.lang.RuntimeException: 无法启动Activity ComponentInfo{com.t.androiduberremake/com.t.androiduberremake.SplashScreenActivity}java.lang.IllegalStateException在此进程com.t.androiduberremake中未初始化默认的FirebaseApp请确保首先调用FirebaseApp.initializeApp(Context) first
  2. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3760)
  3. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)
  4. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
  5. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
  6. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)
  8. at android.os.Handler.dispatchMessage(Handler.java:107)
  9. at android.os.Looper.loop(Looper.java:213)
  10. at android.app.ActivityThread.main(ActivityThread.java:8147)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
  13. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
  14. Caused by: java.lang.IllegalStateException: 在此进程com.t.androiduberremake中未初始化默认的FirebaseApp请确保首先调用FirebaseApp.initializeApp(Context) first
  15. at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
  16. at com.google.firebase.auth.FirebaseAuth.getInstance(com.google.firebase:firebase-auth@@19.3.2:1)
  17. at com.t.androiduberremake.SplashScreenActivity.init(SplashScreenActivity.java:49)
  18. at com.t.androiduberremake.SplashScreenActivity.onCreate(SplashScreenActivity.java:39)
  19. at android.app.Activity.performCreate(Activity.java:8066)
  20. at android.app.Activity.performCreate(Activity.java:8054)
  21. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
  22. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3733)
  23. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)
  24. ...
  25. 2020-08-13 15:33:55.746 12265-12265/? I/Process: Sending signal. PID: 12265 SIG: 9
英文:

When I run the app I get an error while launching the app. It shows me that I forgot to do something with FireBase and it shows me this as well: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.t.androiduberremake. Make sure to call FirebaseApp.initializeApp(Context) first.

there&#39;s my code

  1. private final static int LOGIN_REQUEST_CODE =7171;
  2. private List&lt;AuthUI.IdpConfig&gt; providers;
  3. private FirebaseAuth firebaseAuth;
  4. private FirebaseAuth.AuthStateListener listener;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. FirebaseApp.initializeApp(this);
  9. init();
  10. }
  11. private void init() {
  12. providers = Arrays.asList(
  13. new AuthUI.IdpConfig.PhoneBuilder().build(),
  14. new AuthUI.IdpConfig.GoogleBuilder().build());
  15. firebaseAuth =FirebaseAuth.getInstance();
  16. listener = myFirebaseAuth -&gt;{
  17. FirebaseUser user = myFirebaseAuth.getCurrentUser();
  18. if(user != null)
  19. delaySplashScreen();
  20. else
  21. showLoginLayout();
  22. };
  23. }
  24. private void showLoginLayout() {
  25. AuthMethodPickerLayout authMethodPickerLayout = new AuthMethodPickerLayout
  26. .Builder(R.layout.layout_sign_in).setPhoneButtonId(R.id.btn_phone_sign_in)
  27. .setGoogleButtonId(R.id.btn_google_sign_in).build() ;
  28. startActivityForResult(AuthUI.getInstance()
  29. .createSignInIntentBuilder()
  30. .setAuthMethodPickerLayout(authMethodPickerLayout)
  31. .setIsSmartLockEnabled(false).setAvailableProviders(providers)
  32. .build(), LOGIN_REQUEST_CODE);
  33. }
  34. private void delaySplashScreen() {
  35. Completable.timer(5, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
  36. .subscribe(() -&gt; Toast.makeText(SplashScreenActivity.this, &quot;Splash Screen done!! &quot;, Toast.LENGTH_SHORT).show());
  37. }
  38. @Override
  39. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  40. super.onActivityResult(requestCode, resultCode, data);
  41. if(requestCode == LOGIN_REQUEST_CODE){
  42. IdpResponse response = IdpResponse.fromResultIntent(data);
  43. if(resultCode == RESULT_OK){
  44. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  45. }
  46. else {
  47. Toast.makeText(this, &quot;[ERROR]: &quot;+ response.getError().getMessage(), Toast.LENGTH_SHORT).show();
  48. }
  49. }
  50. }

that is what console shows me when I try to run a given app:

  1. 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.
  2. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3760)
  3. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)
  4. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
  5. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
  6. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)
  8. at android.os.Handler.dispatchMessage(Handler.java:107)
  9. at android.os.Looper.loop(Looper.java:213)
  10. at android.app.ActivityThread.main(ActivityThread.java:8147)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
  13. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
  14. Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.t.androiduberremake. Make sure to call FirebaseApp.initializeApp(Context) first.
  15. at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
  16. at com.google.firebase.auth.FirebaseAuth.getInstance(com.google.firebase:firebase-auth@@19.3.2:1)
  17. at com.t.androiduberremake.SplashScreenActivity.init(SplashScreenActivity.java:49)
  18. at com.t.androiduberremake.SplashScreenActivity.onCreate(SplashScreenActivity.java:39)
  19. at android.app.Activity.performCreate(Activity.java:8066)
  20. at android.app.Activity.performCreate(Activity.java:8054)
  21. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
  22. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3733)
  23. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)&#160;
  24. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)&#160;
  25. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)&#160;
  26. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)&#160;
  27. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)&#160;
  28. at android.os.Handler.dispatchMessage(Handler.java:107)&#160;
  29. at android.os.Looper.loop(Looper.java:213)&#160;
  30. at android.app.ActivityThread.main(ActivityThread.java:8147)&#160;
  31. at java.lang.reflect.Method.invoke(Native Method)&#160;
  32. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)&#160;
  33. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)&#160;
  34. 2020-08-13 15:33:55.746 12265-12265/? I/Process: Sending signal. PID: 12265 SIG: 9

答案1

得分: 1

你还没有将google-services插件添加到你的build.gradle中。这很重要。
根据文档,你还需要添加:

  1. apply plugin: 'com.google.gms.google-services'
英文:

You haven't added the google-services plugin to your build.gradle. Its important.
According to the documentation, you also have to add:

  1. apply plugin: &#39;com.google.gms.google-services&#39;

huangapple
  • 本文由 发表于 2020年8月13日 18:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63393441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定