简体   繁体   中英

Firebase Assistant: Realtime Database

I'm new at android developing. Here's my code that I copied on the firebase assistant

import android.nfc.Tag;
import android.util.Log;

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;

public class FirebaseDB {
    DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();

    myRef.addValueEventListener(new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnopshot){
            String value = dataSnopshot.getValue(String.class);
            Log.d(Tag, "Value is: " + value);
        }

       @Override
       public void onCancelled(DatabaseError error) {
           Log.w(Tag, "Failed to read value.", error.toException());
       }
   });
}

I'm using android studio 2.3.3

My problem is that, the myRef.addValueEventListener became error though myRef is a DatabaseReference which inherit Query class. I tried DatabaseReference myRef = new DatabaseReference(); still got me an error, also the new ValueEventListener() , DataSnopshot dataSnopshot and DatabaseError error , same also for the Tag

Here's for the build.gradle, and the firebase assistant

在此处输入图片说明

Thanks for the help.

This looks like a firebase and appcompat dependency issue. firebase 10.0.2 is very old now. The latest version is

compile 'com.google.firebase:firebase-database:11.2.2

Also you buildtools version is 26, which conflicts with your targetsdk version 24

Could be that your errors are caused by sdk tools and libraries conflicts in your gradle. So try to update your support libraries versions to latest which is currently 26.0.0-alpha1 or at least 25.3.1 .

For example compile 'com.android.support:appcompat-v7:25.3.1'

Then update your firebase dependencies to latest version 11.2.2 .

Note: after upgrading firebase libraries there are some new rules

  • your compileSdkVersion must be 26 also upgrade your targetSdkVersion to 26
  • inside your build gradle add path for your dependencies which are now directly available on maven.google.com like this:

allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } } }

For more see on: https://firebase.googleblog.com/2017/08/some-updates-to-apps-using-google-play.html

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