Cannot Create an Instance of Class Viewmodel

Cannot create an instance of class ViewModel

Make your constructor public.

Cannot create an instance of class view model - Kotlin

You should add @HiltViewModel annotation on top of your view model.

Cannot create an instance of class ViewModel class

You need to use ViewModelFactory. Because there is "demoRepository" in your primary builder.

    class DemoViewModelFactory constructor(private val repository:DemoImpl): ViewModelProvider.Factory {

override fun <T : ViewModel> create(modelClass: Class<T>): T {
return if (modelClass.isAssignableFrom(DemoViewModel::class.java!!)) {
DemoViewModel(this.repository) as T
} else {
throw IllegalArgumentException("ViewModel Not Found")
}
}
}

Usage

viewModel = ViewModelProvider(this, DemoViewModelFactory(repositoryObject)).get(DemoViewModel::class.java)

Cannot create an instance of ViewModel Class

Let me start off by saying CodeInFlow is a great source to learn from I've used him lots to help build my knowledge, Codelabs are also great and frequently updated by the Android dev team. If you are starting out I would recommend learning Kotlin over java, Kotlin is the future of android and recommend language by the Android Dev Team.

This is how you get a new or existing ViewModel.

NoteViewModel viewModel = new ViewModelProvider(this).get(NoteViewModel.class);

Using a custom ViewModel Factory is not necessary for this project. You would only want to use custom ViewModel Factory if you are passing an argument into the ViewModel constructor.

Cannot create an instance of class ViewModel with constructor

You can use AndroidViewModel

class MyViewModel@Inject constructor(val application: Application) : AndroidViewModel(application)

Why it says Cannot create an instance of class com.app.myapp.viewModel in android jetpack compose?

Reasons may cause system can not create viewModel:

  • Your viewModel class is not Public

  • Your package name which contains viewModel contains special keywords (such a "package.new.feature")

  • If you are using dagger hilt you should putt annotation @HiltViewModel above the class declaration and create constructor like

    @HiltViewModel
    class viewModel @Inject constructor() : ViewModel()
  • With the dagger hilt You should use hiltViewModel() function to create instance for compose instead of viewModel()

dependency: androidx.hilt:hilt-navigation-compose

   @Composable
fun MyExample (viewModel: MyViewModel = hiltViewModel())
  • Your ViewModel class does not extend from androidx.lifecycle.ViewModel

Cannot create an instance of ViewModel class (Unable to start activity ComponentInfo)

Quoting the documentation for AndroidViewModel:

Subclasses must have a constructor which accepts Application as the only parameter.

Your constructor does not meet that requirement.

Either:

  • Remove the Context context and LifecycleOwner lifecycleOwner constructor parameters from your HomeViewModel, or

  • Create a ViewModelProvider.Factory that can build your HomeViewModel instances, and use that factory with ViewModelProviders.of()

Cannot create an instance of ViewModel

While using Hilt along with Navigation Library, you need to use hiltViewModel() instead of viewModel() to inject ViewModels. Check out documentation for more info.

Cannot create an instance of class ViewModel unable to find cause

@ViewModelInject has been deprecated. You can check here for more info.

In newer versions, You need to annotate viewmodel class with @AndroidViewModel and constructor with @Inject.

@AndroidViewModel
class SongViewModel @Inject constructor(
musicServiceConnection: MusicServiceConnection
) : ViewModel()

Hope it would solve this issue.



Related Topics



Leave a reply



Submit