Roy Smith
Roy Smith

Reputation: 2163

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

I'm following along with the ViewModel example given here. When I try to compile something like the "ViewModel would be:" code, ie:

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

I get the error:

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

which is not surprising because LiveData is indeed abstract.

So, 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? Based on the 50k google hits you get when searching for that error text, I'm not the first person to be puzzled by this.

Note: related question

Upvotes: 0

Views: 262

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

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"? I would use "And" — it is just wrong, and there is an implication that this should be some subclass of LiveData.

FWIW, I just filed an issue to get this addressed.

Upvotes: 2

Related Questions