简体   繁体   English

firebase 实时数据库无法写入数据

[英]firebase realtime database unable to write data

I added firebase through tools Firebase, and both Connect your app to firebase and Add the realtime database to your app showing green tick marks.我通过工具 Firebase 添加了 firebase,并将您的应用程序连接到 firebase 并将实时数据库添加到您的应用程序,显示绿色刻度线。 indicates successfully attached.表示附加成功。 But getting error which i included in my Logcat error但是出现错误,我将其包含在我的 Logcat 错误中

firebase realtime database file: firebase实时数据库文件:

get-set-data-firebase-22217-default-rtdb: null

project level gradle file:项目级 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
}

app level gradle file:应用程序级别 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 file: 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!")
    }
}

Here is my Logcat error File:这是我的 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)

EDIT: classpath 'com.google.gms:google-services:4.3.10'编辑: classpath 'com.google.gms:google-services:4.3.10'

Fixes the problem解决问题


I have a project that just started getting this error, been using Firebase for years.我有一个项目刚开始出现此错误,多年来一直使用 Firebase。

I tired all of the fixes, created an Application Class and init Firebase there.我厌倦了所有的修复,在那里创建了一个应用程序 Class 和 init Firebase。 Didn't help.没有帮助。 What did help was:有帮助的是:

classpath 'com.google.gms:google-services:4.3.9'

Change it to改成

classpath 'com.google.gms:google-services:4.3.8'

and see if it works.看看它是否有效。

You are setting FirebaseApp.initializeApp(**) with activity context.您正在使用活动上下文设置 FirebaseApp.initializeApp(**) 。 You should be setting it with application context.您应该使用应用程序上下文进行设置。

FirebaseApp.initializeApp(getApplicationContext())

You probably don't need to use FirebaseApp unless you are working with more than one firebase app in a single project.您可能不需要使用 FirebaseApp,除非您在一个项目中使用多个 firebase 应用程序。 I would remove FirebaseApp completely unless you need it.除非您需要,否则我会完全删除 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!")
    }
}

You should also provide a key for a value, try this.您还应该为一个值提供一个键,试试这个。

database.child(UUID.randomUUID().toString()).setValue("Hello, World!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM