本文介绍了Firebase错误:无法添加ValueEventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的声明代码:

  database = FirebaseDatabase.getInstance(); 
myRef = database.getReference(lifts);

为什么我不能添加 ValueEventListener ?我尝试了Firebase指南中的示例。这里是代码:

  myRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){

}

@Override
public void onCancelled(FirebaseError firebaseError){

}
});

这个工作,我不知道为什么?

  myRef.addValueEventListener(new com.google.firebase.database.ValueEventListener(){
@Override
public void onDataChange(com.google.firebase.database .DataSnapshot dataSnapshot){
for(com.google.firebase.database.DataSnapshot item:dataSnapshot.getChildren()){
...
}
...
}
});


解决方案

错误在于,您必须使用 com.google.firebase.database.ValueEventListener 。但是我认为你在导入文件的顶部有一个 com.firebase.client.ValueEventListener 的导入。因此,编译器会尝试使用 com.firebase.client.ValueEventListener 作为默认值,这显然会失败。

要解决这个问题,请查看您的导入并删除 com.firebase.client.ValueEventListener 的条目。然后,您可以添加 com.google.firebase.database.ValueEventListener



如果您拥有此方法使用他们两个。然后,除了使用全名(例如 com.google.firebase.database.ValueEventListener 而不是 ValueEventListener >)为两者或至少一个。


Here is my declaration code:

database = FirebaseDatabase.getInstance();
myRef = database.getReference("lifts");

Why can't I add the ValueEventListener? I tried the sample from the Firebase Guide. Here is the code:

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

This works and I have no idea why?

myRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
    @Override
    public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
        for (com.google.firebase.database.DataSnapshot item: dataSnapshot.getChildren()) {
            ...
        }
        ...
    }
});    
解决方案

The error is, that you have to use a com.google.firebase.database.ValueEventListener. But I think you have an import for com.firebase.client.ValueEventListener in your imports on top of the file.

Because of this, the compiler tries to use com.firebase.client.ValueEventListener as default, which obviously fails.

To fix this, look through your imports and remove the entry for com.firebase.client.ValueEventListener. Then you can add com.google.firebase.database.ValueEventListener

This approach doesn't work if you have to use both of them. Then there's no other solution than using the full names (e.g. com.google.firebase.database.ValueEventListener instead of just ValueEventListener) for both or at least one.

这篇关于Firebase错误:无法添加ValueEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:24