文档暗示你可以实例化 LiveData,但你可以,因为它是抽象的

Docs imply you can instantiate LiveData, but you can because it's abstract

我跟着 ViewModel example given here。当我尝试编译类似 "ViewModel would be:" 的代码时,即:

public final LiveData<User> userLiveData = new LiveData<>();

我收到错误:

Error:(7, 52) error: LiveData is abstract; cannot be instantiated

这并不奇怪,因为 LiveData 确实是抽象的。

所以,我不明白他们给出的示例代码。只是错了吗?或者是否有一个暗示,"No, of course you're not going to do this, you're going to subclass LiveData",在这种情况下它只是非常误导?根据您在搜索该错误文本时得到的 50k google hits,我不是第一个对此感到困惑的人。

注:

I don't understand the sample code they give. It it just wrong? Or is there an implied, "No, of course you're not going to do this, you're going to subclass LiveData", in which case it's just terribly misleading?

"Or"?我会使用 "And" — 这是错误的,并且暗示这应该是 LiveData.

的某个子类

FWIW,我刚刚提交 an issue 来解决这个问题。