gpt4 book ai didi

java - 确保先调用 FirebaseApp.initializeApp(Context)

转载 作者:行者123 更新时间:2023-11-29 14:18:36 28 4
gpt4 key购买 nike

它可能已经与其他问题完全相同,但我真的不知道这里出了什么问题。预先感谢您的帮助。

build.gradle(项目)

        // 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(模块)

    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'

主 Activity .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");

错误:

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) 

它可能已经与其他问题完全相同,但我真的不知道这里出了什么问题。预先感谢您的帮助。

最佳答案

对我来说,任何地方给出的解决方案都没有用。只有这个有效。只需将我的谷歌服务从 4.1.0 下载到 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 */
}

因此,如果您更新了 google 服务,只需尝试降级或更改为旧版本即可。希望它能帮助别人。

关于java - 确保先调用 FirebaseApp.initializeApp(Context),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887880/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com