2
我试图初始化我LiveData对象,并给出了错误: “LiveData是抽象的,它不能被实例化”LiveData是抽象的Android
LiveData listLiveData =新LiveData <>();
我试图初始化我LiveData对象,并给出了错误: “LiveData是抽象的,它不能被实例化”LiveData是抽象的Android
LiveData listLiveData =新LiveData <>();
因为它是抽象的(如@CommonsWare说),你需要把它扩大到一个子类,然后在表格需要覆盖的方法:
public class LiveDataSubClass extends LiveData<Location> {
}
在一个视图模型,您可能需要使用MutableLiveData
。
例如为:
class MyViewModel extends ViewModel {
private MutableLiveData<String> data = new MutableLiveData<>();
public LiveData<String> getData() {
return data;
}
public void loadData() {
// Do some stuff to load the data... then
data.setValue("new data"); // Or use data.postValue()
}
}
你是如何初始化的对象?请包括您的代码,以便我们可以看到发生了什么。 –
恩,这是因为'LiveData'是'abstract'。你在这里试图用'LiveData'做什么?您可能需要创建“LiveData”的子类或使用“MutableLiveData”。 – CommonsWare