Make sure to call FirebaseApp.initializeApp(Contex

2020-02-15 06:01发布

It might be really same with other questions already but I don't really know what's wrong in here. Thanks in advance for the help.

build.gradle (project)

        // Top-level build file where you can add configuration options common to all sub-projects/modules.

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

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

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

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

build.gradle(module)

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.bustracker.usc.myapplication"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    compile 'com.google.android.gms:play-services-maps:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
    compile 'com.google.firebase:firebase-database:10.2.6'
    testCompile 'junit:junit:4.12'
}

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

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FirebaseApp.initializeApp(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        firebaseDatabase = FirebaseDatabase.getInstance();
        mRootReference = firebaseDatabase.getReference();
        mheadingReference = mRootReference.child("users");

ERROR:

FATAL EXCEPTION: main
                                                 Process: com.bustracker.sample.myapplication, PID: 2658
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bustracker.sample.myapplication/com.bustracker.sample.myapplication.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bustracker.sample.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                  Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bustracker.sample.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                     at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                     at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                     at com.bustracker.sample.myapplication.MainActivity.onCreate(MainActivity.java:53)
                                                     at android.app.Activity.performCreate(Activity.java:6679)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:154) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

It might be really same with other questions already but I don't really know what's wrong in here. Thanks in advance for the help.

4条回答
Animai°情兽
2楼-- · 2020-02-15 06:27

I don't know why this worked for me but removing

tools:node="replace" 

from my AndroidManifest.xml improved the crash from right away to from some other thing. Hope I helped someone.

查看更多
SAY GOODBYE
3楼-- · 2020-02-15 06:43

According to the docs:

As said in the docs:

Any FirebaseApp initialization must occur only in the main process of the app. Use of Firebase in processes other than the main process is not supported and will likely cause problems related to resource contention.

you need to initialize it not in the activity.

add an application class to your manifest example:

      <applicaton
       android:name="MyApplication"

then do this:

public class MyApplication extends Application {
  @Override
public void onCreate() {
    super.onCreate();
   FirebaseApp.initializeApp(this);
}

and remove the initialization from the activity. You need to initialize it in the application class which is the base class.

Edit(about application):

Base class for maintaining global application state. You can provide your own implementation by creating a subclass and specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's tag. The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

Useful link: https://developer.android.com/reference/android/app/Application.html

查看更多
淡お忘
4楼-- · 2020-02-15 06:48

For me none of the solution worked that were given any where. Only this worked. Just had to download grade my google services from 4.1.0 to 4.0.0

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
    classpath 'com.google.gms:google-services:4.0.0'
    /*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}

So if you have updated the google services, just try to downgrade or change to an older version. Hope it helps someone else.

查看更多
仙女界的扛把子
5楼-- · 2020-02-15 06:51

For me, it was enough to ensure that the google-services plugin is initialized in my app.gradle:

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

Then I did not even have to call FirebaseApp.initializeApp(Context) at all. Maybe it helps someone.

查看更多
登录 后发表回答