java.lang.IllegalStateException:此过程未初始化默认的FirebaseApp

时间:2019-01-23 10:08:14

标签: java android firebase illegalstateexception

我创建了一个新项目,并通过firebase的android studio工具添加了firebase。按照指示进行了所有操作,但在午餐期间我遇到了此错误。

Process: com.chitchat, PID: 20084
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chitchat/com.chitchat.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.chitchat. Make sure to call FirebaseApp.initializeApp(Context) first.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.chitchat. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
    at com.chitchat.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:7144)
    at android.app.Activity.performCreate(Activity.java:7135)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6718) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

MainActivity.java是

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    FirebaseApp.initializeApp(MainActivity.this);
    mAuth = FirebaseAuth.getInstance();
}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    //updateUI(currentUser);

    if(currentUser == null){
        Intent startAct = new Intent(MainActivity.this, Start_Activity.class);
        startActivity(startAct);
        finish();
    }
}
}

和gradle.build是

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.chitchat"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    //implementation 'com.google.android.gms:play-services-base'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


}

项目级gradle.build

    buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.google.gms:google-services:4.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

在网络上找不到任何内容!添加了firebaseApp.initializeApp(this),该指令在任何说明中都没有,但是在模拟器和设备上仍然是相同的错误。

4 个答案:

答案 0 :(得分:2)

我也遇到了这个问题。最近几周我看到很多人都遇到了问题。

        classpath 'com.android.tools.build:gradle:3.3.0'

将其更改为

        classpath 'com.android.tools.build:gradle:3.2.0'

here所述!

因为根据这次讨论here,AGP遇到了一些问题! 我认为这可以解决您的问题。

答案 1 :(得分:0)

将此添加到build.gradle的底部:

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

答案 2 :(得分:0)

首先,您需要在root级build.gradle上添加com.google.gms:google-services:x.x.x。 像这样

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.1'
    classpath 'com.google.gms:google-services:3.0.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

然后在build.gradle(app)的底部中添加Google插件。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    //implementation 'com.google.android.gms:play-services-base'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


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

答案 3 :(得分:0)

在您的Application类而不是Activity中添加以下代码,以便将Firebase初始化为整个应用程序,而不仅仅是一个Activity

@Override
public void onCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);
}

此外,您需要将应用插件代码从顶部移至底部 引用:Add Firebase SDK

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

它要求apply plugin: 'com.google.gms.google-services'行位于app/build.gradle文件的底部处,以便不引入依赖项冲突。您可以通过运行./gradlew :app:dependencies来查看此步骤的结果。 Reference

另外,请尝试将所有firebase和gms:google-services依赖项同步到最新版本。