FirebaseAuth.getInstance返回null

问题描述 投票:0回答:2

我刚开始使用火力地堡认证,我跟着一些教程,包括火力地堡的文档,但无论我做什么,FirebaseAuth.getInstance()始终返回null。想了很久之后,我决定删除我所做的一切相关的认证,并开始过来,让我只用FirebaseAuth变量的声明和初始化,如下所示:在类变量初始化:

private FirebaseAuth mAuth;

然后在onCreate()方法我叫其初始化火力的变量和引用的方法,所以我把下面的方法:

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

在火力地堡认证网站本身,我已经启用的登录方法的认证服务。

所以,如果有人能告诉我这里有什么问题,我很乐意,因为我已经花了近6小时试图弄明白...

java android firebase firebase-authentication
2个回答
0
投票

的getInstance()是用于检查是否用户已登录与否。空意味着你还没有登录。


0
投票

documentation至少告诉,FirebaseApp.initializeApp(this);返回什么:

默认FirebaseApp,如果任一先前已初始化,或者火力地堡API密钥存在于字符串资源。否则返回null

确保在build.gradle底部应用谷歌服务的插件,在android块之后......因为这是那些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

© www.soinside.com 2019 - 2024. All rights reserved.