简体   繁体   中英

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. indicates successfully attached. But getting error which i included in my Logcat error

firebase realtime database file:

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

project level gradle file:

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:

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:

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:

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'

Fixes the problem


I have a project that just started getting this error, been using Firebase for years.

I tired all of the fixes, created an Application Class and init Firebase there. 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. 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. I would remove FirebaseApp completely unless you need it.

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!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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