[英]FirebaseAuth.getInstance returns null
I have just started using Firebase Authentication, I followed few tutorials including Firebase's docs, but no matter what I do, FirebaseAuth.getInstance() always returns null.我刚刚开始使用 Firebase 身份验证,我遵循了一些教程,包括 Firebase 的文档,但无论我做什么,FirebaseAuth.getInstance() 总是返回 null。 After trying for a long time, I decided to delete everything I've done related to the Authentication, and start it over, so that I only use the declaration of the FirebaseAuth variable and its initialization, as in the following: In the class variables initialization:
尝试了很长时间,我决定删除我所做的与身份验证相关的所有事情,并重新开始,这样我就只使用 FirebaseAuth 变量的声明及其初始化,如下所示: 在类变量中初始化:
private FirebaseAuth mAuth;
And then in the onCreate() method I called a method which initializes firebase's variables and references, so I call the following method:然后在 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
}
My dependencies in the build.gradle are:我在 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
In the Firebase authentication website itself, I've already enabled the Authentication service in the sign-in method.在 Firebase 身份验证网站本身中,我已经在登录方法中启用了身份验证服务。
So, if anyone can tell me what is wrong here, I'd love that since I've spent the last 6 hours trying to figure it out...所以,如果有人能告诉我这里出了什么问题,我很乐意,因为我花了过去 6 个小时试图弄清楚......
getInstance() is for checking if user is logged in or not. getInstance() 用于检查用户是否登录。 null means you are not logged in.
null 表示您尚未登录。
the documentation at least tells, what FirebaseApp.initializeApp(this);
文档至少说明了什么
FirebaseApp.initializeApp(this);
returns:返回:
the default
FirebaseApp
, if either it has been initialized previously, or Firebase API keys are present in string resources .默认
FirebaseApp
,如果之前已初始化,或者字符串资源中存在 Firebase API 密钥。 Returnsnull
otherwise.否则返回
null
。
make sure to apply the Google Services plugin at the bottom of the build.gradle
, after the android
block... because this is where those API keys in string resources
originate from.确保在
build.gradle
底部应用 Google 服务插件,在android
块之后......因为这是API keys in string resources
中那些API keys in string resources
来源。
which is the only logical explanation for FirebaseAuth.getInstance()
returning null
.这是
FirebaseAuth.getInstance()
返回null
的唯一合乎逻辑的解释。
this one is added twice: com.firebase:firebase-client-android:2.5.2
.这个添加了两次:
com.firebase:firebase-client-android:2.5.2
: com.firebase:firebase-client-android:2.5.2
。
the version mismatch can be solved by exclusion:版本不匹配可以通过排除来解决:
implementation ("com.firebaseui:firebase-ui-database:2.1.1") {
exclude group: "com.android.support", module: "recyclerview-v7"
}
so that it will use com.android.support:recyclerview-v7:28.0.0
instead.这样它将使用
com.android.support:recyclerview-v7:28.0.0
代替。
Add this to you manifest:将此添加到您的清单中:
<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" />
If same service exists, add simpy meta-data tag.如果存在相同的服务,请添加简单的元数据标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.