【问题标题】:FirebaseAuth.getInstance returns nullFirebaseAuth.getInstance 返回 null
【发布时间】:2019-02-02 16:35:38
【问题描述】:

我刚刚开始使用 Firebase 身份验证,我遵循了一些教程,包括 Firebase 的文档,但无论我做什么,FirebaseAuth.getInstance() 总是返回 null。 尝试了半天,我决定把之前做的和Authentication相关的东西都删掉,重新开始,这样我就只用FirebaseAuth变量的声明和初始化了,如下: 在类变量初始化中:

private FirebaseAuth mAuth;

然后在 onCreate() 方法中我调用了一个初始化firebase的变量和引用的方法,所以我调用了以下方法:

private void initFirebase()     //initialize firebase
    {
        FirebaseApp.initializeApp(this);
        mAuth = FirebaseAuth.getInstance();
        mFirebaseDatabase=FirebaseDatabase.getInstance();
        mDatabaseReference=mFirebaseDatabase.getReference().child("users");
        mEventsReference=mFirebaseDatabase.getReference().child("events");
        mStorageRef= FirebaseStorage.getInstance().getReference();
        defaultRef = mStorageRef.child("usersProfilePic/");             //Assigning default reference for storage
    }

我在 build.gradle 中的依赖是:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    /*dataBinding {
        enabled = true;
    }*/
    defaultConfig {
        applicationId "com.example.android.aln4"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        multiDexEnabled true
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    testImplementation 'junit:junit:4.12'

    //google maps api
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.google.android.gms:play-services-places:16.0.0'

    //Firebase
    implementation 'com.google.firebase:firebase-crash:16.2.1'
    implementation 'com.firebase:firebase-client-android:2.5.2'
    implementation 'com.google.firebase:firebase-core:16.0.6'
    implementation 'com.google.firebase:firebase-database:16.0.6'
    implementation 'com.firebase:firebase-client-android:2.5.2'
    implementation 'com.firebaseui:firebase-ui-database:2.1.1'
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.google.firebase:firebase-firestore:18.0.0'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'

    //Calendar
    implementation 'com.github.alamkanak:android-week-view:1.2.6'
    implementation 'com.github.sundeepk:compact-calendar-view:3.0.0'
    implementation 'com.github.khacpv:Calendar-Day-View:1.0.5'
    //noinspection GradleCompatible
    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.android.support:support-v4:28.0.0'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    //Image tools
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    //Validation
    implementation 'com.basgeekball:awesome-validation:2.0'
    //Notification badge
    implementation 'com.nex3z:notification-badge:0.1.0'
    //Others
    implementation 'com.kevin:loopview:1.4.1'

    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'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

在 Firebase 身份验证网站本身,我已经在登录方法中启用了身份验证服务。

所以,如果有人能告诉我这里出了什么问题,我会很高兴的,因为我在过去的 6 个小时里都在试图弄清楚...

【问题讨论】:

  • 你想获取当前登录的用户吗?
  • 很可能google-service.json 丢失或Google Play 服务插件未应用...这需要build.gradle 来回答这个可靠的问题。
  • @ManzurulHoqueRumi 我只是想在 mAuth 变量中获取一些东西,以便以后使用它......真的没关系......
  • @MartinZeitler 我已经重新生成了 google-service.json 文件并替换为之前的文件,并且我应用了 google 服务插件
  • @AdiHarel this 可能不是instanceof Context,上一次调用FirebaseApp.initializeApp(this)。您是在Activity 的范围内调用它吗?因为当它失败时,下面的调用很可能也会失败。

标签: java android firebase firebase-authentication


【解决方案1】:

documentation 至少说明了FirebaseApp.initializeApp(this); 返回的内容:

默认的FirebaseApp,如果之前已初始化,或 Firebase API 密钥存在于字符串资源中。否则返回null

确保在build.gradle 底部的android 块之后应用Google 服务插件...因为这是API keys in string resources 的来源。

这是FirebaseAuth.getInstance()返回null的唯一合乎逻辑的解释。


这个添加了两次:com.firebase:firebase-client-android:2.5.2

版本不匹配可以通过排除解决:

implementation ("com.firebaseui:firebase-ui-database:2.1.1") {
    exclude group: "com.android.support", module: "recyclerview-v7"
}

这样它就会改用com.android.support:recyclerview-v7:28.0.0

【讨论】:

  • 我刚刚将整个 build.gradle 文件添加到原始帖子中,请参阅编辑
  • @AdiHarel 问题是com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true ...这些版本冲突需要先解决 - 因为隐藏问题不会解决问题。
  • 我该怎么做?只是删除这部分?
  • @AdiHarel bediuk。那么它应该抱怨版本不匹配。
  • 我删除了,还是返回null
【解决方案2】:

getInstance() 用于检查用户是否登录。 null 表示您没有登录。

【讨论】:

【解决方案3】:

将此添加到您的清单中:

<service android:name="com.google.firebase.components.ComponentDiscoveryService">
        <meta-data
            android:name="com.google.firebase.components:com.google.firebase.auth.FirebaseAuthRegistrar"
            android:value="com.google.firebase.components.ComponentRegistrar" />

如果存在相同的服务,添加简单的元数据标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多