简体   繁体   English

Firebase Assistant:实时数据库

[英]Firebase Assistant: Realtime Database

I'm new at android developing. 我是android开发的新手。 Here's my code that I copied on the firebase assistant 这是我在Firebase助手上复制的代码

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 我正在使用android studio 2.3.3

My problem is that, the myRef.addValueEventListener became error though myRef is a DatabaseReference which inherit Query class. 我的问题是,尽管myRef是继承Query类的DatabaseReference,但myRef.addValueEventListener还是出错。 I tried DatabaseReference myRef = new DatabaseReference(); 我尝试了DatabaseReference myRef = new DatabaseReference(); still got me an error, also the new ValueEventListener() , DataSnopshot dataSnopshot and DatabaseError error , same also for the Tag 仍然给我一个错误,还有new ValueEventListener()DataSnopshot dataSnopshotDatabaseError error ,对于Tag也是一样

Here's for the build.gradle, and the firebase assistant 这是build.gradle和firebase助手的

在此处输入图片说明

Thanks for the help. 谢谢您的帮助。

This looks like a firebase and appcompat dependency issue. 这看起来像是firebase和appcompat依赖项问题。 firebase 10.0.2 is very old now. firebase 10.0.2现在非常旧。 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 此外,您的buildtools版本为26,与您的targetdk版本24冲突

Could be that your errors are caused by sdk tools and libraries conflicts in your gradle. 可能是您的错误是由sdk工具引起的,并且gradle中的库冲突。 So try to update your support libraries versions to latest which is currently 26.0.0-alpha1 or at least 25.3.1 . 因此,请尝试将support库版本更新为最新版本,当前版本为26.0.0-alpha1或至少25.3.1

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

Then update your firebase dependencies to latest version 11.2.2 . 然后将您的firebase依赖项更新到最新版本11.2.2

Note: after upgrading firebase libraries there are some new rules 注意:升级firebase库后,有一些新规则

  • your compileSdkVersion must be 26 also upgrade your targetSdkVersion to 26 您的compileSdkVersion必须为26也将targetSdkVersion升级为26
  • inside your build gradle add path for your dependencies which are now directly available on maven.google.com like this: 在构建gradle中,为依赖项添加路径,现在可以在maven.google.com上直接使用它,如下所示:

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 有关更多信息,请参见: https : //firebase.googleblog.com/2017/08/some-updates-to-apps-using-google-play.html

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

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