我正在我的应用程序中运行Firebase Firestore。 而且我在调用前两行时得到了Null Pointer Exception:
FirebaseApp.initializeApp(this)
val db = FirebaseFirestore.getInstance()
从build.gradle-项目:
classpath 'com.google.gms:google-services:4.1.0' (also tried 4.0.0)
从build.gradle-应用程序:(当前最新)
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-firestore:17.1.3'
如果我在创建实例时设置断点-我最终会遇到ActivityThread类-registerOnActivityPausedListener方法。 http://take.ms/Fdh7z
代码在其他设备上也可以正常使用。但是在其中一个(Wileyfox Swift 2)上,我得到了NLP并且没有很好的解释:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.firebase.auth.internal.zzab.zzf(int)' on a null object reference
at com.google.firebase.auth.FirebaseAuth.addIdTokenListener(Unknown Source)
at com.google.firebase.firestore.auth.FirebaseAuthCredentialsProvider.<init>(com.google.firebase:firebase-firestore@@17.1.3:76)
at com.google.firebase.firestore.FirebaseFirestore.newInstance(com.google.firebase:firebase-firestore@@17.1.3:109)
at com.google.firebase.firestore.FirestoreMultiDbComponent.get(com.google.firebase:firebase-firestore@@17.1.3:51)
at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.1.3:87)
at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.1.3:72)
at com.myjob.exchange.activity.SplashActivity.onCreate(SplashActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:6723)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
答案 0 :(得分:0)
确保您已将应用程序与Firebase(google-services.json)文件连接,然后尝试执行以下操作:
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
db.setFirestoreSettings(settings);
答案 1 :(得分:0)
问题出在以下几行:
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-firestore:17.1.3'
更改为这些内容后,恢复工作:
'com.google.firebase:firebase-core:16.0.3' implementation 'com.google.firebase:firebase-firestore:17.1.0'
答案 2 :(得分:0)
更改类别
classpath 'com.google.gms:google-services:4.1.0'
TO
classpath 'com.google.gms:google-services:4.2.0'