从Firebase错误中读取错误:ValueEventListener [英] Error in reading from Firebase Error: ValueEventListener
问题描述
我试图使用代码
更新:
在使用完整的类名后仍然有相同的问题: com.google.firebase.database.ValueEventListener postListener = new com.google.firebase.database.ValueEventListener(){...}
(2.X)与新的SDK(9.X)。 Firebase 是传统,为了使用这个新的SDK,我们使用了一个新的SDK来实现这个功能,所有导入都应以 com.google.firebase.database
开始。
传统导入以 com.firebase.client
。
我假设您要使用新的SDK。如果是这样,你的模块build.gradle依赖应该包括:
compile'com.google.firebase:firebase-database:9.4。 0'
不应包含:
compile'com.firebase:firebase-client-android:2.5.0'
I am trying to read from a firebase database using code straight from their documentation. I have my code below and I see the following error when I try to add the ValueEventListener
to the my Firebase reference
variable.
addValueEventListener (com.firebase.client.ValueEventListener) in Query cannot be applied to (com.firebase.client.ValueEventListener)
I am confused why this error occurs because following is the import
statement I use at the top of my Java class file.
import com.google.firebase.database.ValueEventListener;
Basically, I am just trying to get basic read from Firebase running. What am I doing wrong?
public void readfromFireDB() {
Firebase ref = new Firebase("https://my_project.firebaseio.com/");
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Question qn = postSnapshot.getValue(Question.class);
mLog.printToLog("RECEIVED DATA = " + qn.getAnswer() + "," + qn.getLevel());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mLog.printToLog("in onCancelled, database error = " + databaseError.toString());
}
};
ref.addValueEventListener(postListener); //THIS IS WHERE THE ERROR IS
}
UPDATE:
Still having the same issue eve after using the entire classname: com.google.firebase.database.ValueEventListener postListener = new com.google.firebase.database.ValueEventListener() { ... }
You're mixing classes from the legacy SDK (2.X) with the new SDK (9.X). Firebase is legacy, ValueEventListener is new.
To use the new SDK, all of your imports should begin with com.google.firebase.database
.
Legacy imports begin with com.firebase.client
.
I'm assuming you want to use the new SDK. If so, your module build.gradle dependencies should include:
compile 'com.google.firebase:firebase-database:9.4.0'
and should not include:
compile 'com.firebase:firebase-client-android:2.5.0'
这篇关于从Firebase错误中读取错误:ValueEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!