简体   繁体   中英

Android app unable to connect to firebase database

I am trying to connect my write data into my firebase database but it shows error that firebase has not been initialized though I have initialized it.I have already tried changing dependencies etc but still not working

package com.parse.starter;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FirebaseApp.initializeApp(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseApp.initializeApp(this);
        DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
        Map<String,String> values=new HashMap<>();
        values.put("name","Jeff");
        ref.push().setValue(values,new DatabaseReference.CompletionListener()
        {

            @Override
            public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
                if(databaseError==null)
                {
                    Log.i("info","Get to job");
                }
                else
                {
                    Log.i("info","Stay here");
                }
            }
        });
    }
}

gradle file


buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/gradle


apply plugin: 'com.android.application'


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.parse.starter"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-database:16.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.3'
}

There is no need to call:

FirebaseApp.initializeApp(this);

If you performed the integration as explained in the official documentation regarding on how to add Firebase to your Android project .

If you did the above standard integration, Firebase will be initialized automatically when your app starts. See here more details about FirebaseApp .

To solve this, please change the following lines of code:

implementation 'com.google.firebase:firebase-core:16.0.3'

to

implementation 'com.google.firebase:firebase-core:16.0.8'

If you didn't perform a above standard integration, you should call initializeApp at the right moment you need it.

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