Android:“Can not Resolve symbol'addValueEventListener'”在Firebase中 [英] Android: "Cannot Resolve symbol 'addValueEventListener'" in Firebase
问题描述
firebase.addValueEventListener(new ValueEventListener(){
但我总是收到错误:
pre > 无法解析符号'addValueEventListener'
尽管我已经导入了ValueEventListener。 (可能很重要的一点是,ValueEventListener作为一个未使用的导入在IDE中显示,尽管我显然正在尝试使用它)。解决方案
请确保您在方法内部实例化和添加方法,例如OnStart(){}或您正在使用的类的构造函数。如果不是,则它的行为与您描述的类似。 / p>这个问题的一个例子:
pre $ public $ ReadFromFireBase(){
private DatabaseReference mDatabase;
$ b $ public ReadFromFireBase(){
}
ValueEve ntListener postListener = new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//获取Post对象并使用这些值更新UI
Post post = dataSnapshot.getValue(Post.class);
//
}
@Override
public void onCancelled(DatabaseError databaseError){
//获取发布失败,记录消息
Log.w(TAG,loadPost:onCancelled,databaseError.toException());
// ...
}
};
mDataBase.addValueEventListener(postListener);
$ b}
这不会工作,并抛出类似于你所得到的错误,这是一个容易的错误,使得听者的结构
正确的方法是这样的作为:
public class ReadFromFireBase(){
private DatabaseReference mDatabase;
私人的ValueEventListener postListener;
$ b public ReadFromFireBase(){
postListener = new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
//获取Post对象并使用这些值来更新UI
Post post = dataSnapshot.getValue(Post.class);
// ...
}
@Override
public void onCancelled(DatabaseError databaseError){
//获取发布失败,记录消息
Log.w(TAG,loadPost:onCancelled,databaseError.toException());
// ...
}
};
mDataBase.addValueEventListener(postListener);
}
$ b}
I've tried importing a variety of Firebase packages to stop this error from occurring, but it still persists. I'm trying to do something along the lines of:
firebase.addValueEventListener(new ValueEventListener() {
but I'm consistently getting the error:
Cannot Resolve symbol 'addValueEventListener'
despite the fact I have ValueEventListener imported. (It might be important to note that ValueEventListener is displayed in the IDE as an unused import, even though I'm clearly attempting to use it)
Make sure that you are instantiating and adding the method inside a method such as OnStart(){} or the constructor of the class you are working in. If it is not it will behave similar to how you describe.
An Example of this issue :
public class ReadFromFireBase(){
private DatabaseReference mDatabase;
public ReadFromFireBase(){
}
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mDataBase.addValueEventListener(postListener);
}
This will not work and throw errors similar to what you are getting, it is an easy mistake to make given the way the listener is structured
The proper way would be something such as :
public class ReadFromFireBase(){
private DatabaseReference mDatabase;
private ValueEventListener postListener;
public ReadFromFireBase(){
postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mDataBase.addValueEventListener(postListener);
}
}
这篇关于Android:“Can not Resolve symbol'addValueEventListener'”在Firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!