变量 'observer' 可能尚未初始化
Variable 'observer' might not have been initialized
我试图在第一次回调后移除观察者。我运气不好。
Observer <? super List<MyEntity>> observer = new Observer<List<MyEntity>>() {
@Override
public void onChanged(List<MyEntity> myEntities) {
Log.d(TAG, "observer changed");
//Variable 'observer' might not have been initialized error here
MySearchViewModel.getMyList().removeObserver(observer);
}
};
MySearchViewModel.getMyList().observe(MainActivity.this, observer);
我该如何解决这个问题?
感谢 mrteddy。这是正确的代码:
Observer <? super List<MyEntity>> observer = new Observer<List<MyEntity>>() {
@Override
public void onChanged(List<MyEntity> myEntities) {
MySearchViewModel.getMyList().removeObserver(this);
}
};
MySearchViewModel.getMyList().observe(MainActivity.this, observer);
我试图在第一次回调后移除观察者。我运气不好。
Observer <? super List<MyEntity>> observer = new Observer<List<MyEntity>>() {
@Override
public void onChanged(List<MyEntity> myEntities) {
Log.d(TAG, "observer changed");
//Variable 'observer' might not have been initialized error here
MySearchViewModel.getMyList().removeObserver(observer);
}
};
MySearchViewModel.getMyList().observe(MainActivity.this, observer);
我该如何解决这个问题?
感谢 mrteddy。这是正确的代码:
Observer <? super List<MyEntity>> observer = new Observer<List<MyEntity>>() {
@Override
public void onChanged(List<MyEntity> myEntities) {
MySearchViewModel.getMyList().removeObserver(this);
}
};
MySearchViewModel.getMyList().observe(MainActivity.this, observer);