[英]firebase realtime database unable to write data
我通過工具 Firebase 添加了 firebase,並將您的應用程序連接到 firebase 並將實時數據庫添加到您的應用程序,顯示綠色刻度線。 表示附加成功。 但是出現錯誤,我將其包含在我的 Logcat 錯誤中
firebase實時數據庫文件:
get-set-data-firebase-22217-default-rtdb: null
項目級 gradle 文件:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
classpath 'com.google.gms:google-services:4.3.9'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
應用程序級別 gradle 文件:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
android {
compileSdk 30
defaultConfig {
applicationId "com.example.firebasefetch"
minSdk 21
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'com.google.firebase:firebase-database-ktx:20.0.1'
implementation 'com.google.firebase:firebase-auth-ktx:21.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
MainActivity.kt 文件:
package com.example.firebasefetch
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.FirebaseApp
import com.google.firebase.database.FirebaseDatabase
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirebaseApp.initializeApp(this)
val database = FirebaseDatabase.getInstance().reference
database.setValue("Hello, World!")
}
}
這是我的 Logcat 錯誤文件:
2021-08-06 22:57:51.467 8980-8980/? E/e.firebasefetc: Unknown bits set in runtime_flags: 0x8000
2021-08-06 22:57:52.207 8980-9018/com.example.firebasefetch E/Perf: Fail to get file list com.example.firebasefetch
2021-08-06 22:57:52.208 8980-9018/com.example.firebasefetch E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2021-08-06 22:57:52.209 8980-9018/com.example.firebasefetch E/Perf: Fail to get file list com.example.firebasefetch
2021-08-06 22:57:52.209 8980-9018/com.example.firebasefetch E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2021-08-06 22:57:52.553 8980-8980/com.example.firebasefetch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firebasefetch, PID: 8980
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firebasefetch/com.example.firebasefetch.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.firebasefetch. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
編輯: classpath 'com.google.gms:google-services:4.3.10'
解決問題
我有一個項目剛開始出現此錯誤,多年來一直使用 Firebase。
我厭倦了所有的修復,在那里創建了一個應用程序 Class 和 init Firebase。 沒有幫助。 有幫助的是:
classpath 'com.google.gms:google-services:4.3.9'
改成
classpath 'com.google.gms:google-services:4.3.8'
看看它是否有效。
您正在使用活動上下文設置 FirebaseApp.initializeApp(**) 。 您應該使用應用程序上下文進行設置。
FirebaseApp.initializeApp(getApplicationContext())
您可能不需要使用 FirebaseApp,除非您在一個項目中使用多個 firebase 應用程序。 除非您需要,否則我會完全刪除 FirebaseApp。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// FirebaseApp.initializeApp(this)
val database = FirebaseDatabase.getInstance().reference
database.setValue("Hello, World!")
}
}
您還應該為一個值提供一個鍵,試試這個。
database.child(UUID.randomUUID().toString()).setValue("Hello, World!")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.