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 ofviewModel()
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
andLifecycleOwner lifecycleOwner
constructor parameters from yourHomeViewModel
, orCreate a
ViewModelProvider.Factory
that can build yourHomeViewModel
instances, and use that factory withViewModelProviders.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
How Can Retrofit 2.0 Parse Nested JSON Object
What Actually Happens When Persistence Is Enabled in Firebase
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
Android 4.4 Giving Err_Cache_Miss Error in Onreceivederror for Webview Back
How to Add Parameters to API (Http Post) Using Okhttp Library in Android
Httpclient on Android:Nohttpresponseexception Through Umts/3G
Using the New "Manifestmerger" Property in Android
Gcm Push Notification with ASP.NET
Network_Provider Not Providing Updated Locations
Do Gcm Registration Id's Expire
How to Apply Plugin to Only One Flavor in Gradle
How to Check If Running on UI Thread in Android
Android - Loop Part of the Code Every 5 Seconds
Android Maps API V2 Draw Circle