简体   繁体   中英

get Instance() method is not being accepted in android studio to fetch firebase data

I am creating an app based on firebase database. I have followed all steps shown on google. Everything working well except red Ink on getInstace () and getReference() method.

private DatabaseReference rfr;
// code to fetch it
 rfr = FirebaseDatabase.getInstance().getReference().child("Progress");//in this line//
                .orderByChild("rollnum")
                .equalTo(301);

I cant guess where is the error. Please help me.

I have rechecked everything .I could not find any error. Do we have to create helper class like sqlite?

Here are my build gradle files:-

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.google.gms:google-services:4.3.4'
        // 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
}

and app level build gradle:-

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example_my pakage name"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 3
        versionName "1.2"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.google.firebase:firebase-database:19.5.1'
    implementation platform('com.google.firebase:firebase-bom:26.0.0')
    implementation 'com.google.firebase:firebase-firestore'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-auth'
    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
    implementation 'com.android.support:multidex:1.0.3'
}

I am not really sure why it is not working. I have actually tried also making a project using Firebase in Android Studio, and it has worked pretty well for me.

One thing I would check is that all of your import statements are correct:

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

Also, make sure that you include whatever keys are necessary in the app-level gradle files. You need to be sure that the dependencies are correctly installed in the project and also that you imported them in the scripts. Other than that, based on the code that you sent, I do not see a reason why it doesn't work.

mDatabase = FirebaseDatabase.getInstance().getReference("Message");

This is the exact code that I used in my project and it seemed to work just fine. Based on the code you send, I do not really see an issue. The only suggestion I have is to double check your imports and gradle files.

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