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

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

Default FirebaseApp is not initialized android studio

问题

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

以下是您的代码:

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: 无法启动Activity ComponentInfo{com.t.androiduberremake/com.t.androiduberremake.SplashScreenActivity}java.lang.IllegalStateException在此进程com.t.androiduberremake中未初始化默认的FirebaseApp请确保首先调用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: 在此进程com.t.androiduberremake中未初始化默认的FirebaseApp请确保首先调用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)
    ...
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

private final  static int LOGIN_REQUEST_CODE =7171;
private List&lt;AuthUI.IdpConfig&gt; 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 -&gt;{
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(() -&gt; Toast.makeText(SplashScreenActivity.this, &quot;Splash Screen done!! &quot;, 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, &quot;[ERROR]: &quot;+ response.getError().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}  

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

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)&#160;
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)&#160;
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)&#160;
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)&#160;
at android.os.Handler.dispatchMessage(Handler.java:107)&#160;
at android.os.Looper.loop(Looper.java:213)&#160;
at android.app.ActivityThread.main(ActivityThread.java:8147)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)&#160;
2020-08-13 15:33:55.746 12265-12265/? I/Process: Sending signal. PID: 12265 SIG: 9

答案1

得分: 1

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

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:

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:

确定