I'm trying to initialize viewmodel in Fragment but each time I want to pass the interface in my viewmodel constructor it throws the error Cannot create an instance of class ViewModel Also I don't have any problem in kotlin-kapt or any lifecycle annotations
here is my ViewModel class
class SettingsViewModel (
var settingsView: SettingsView
) : ViewModel(){ }
and here is my fragment where i want to initialize the viewmodel
class SettingsFragment : Fragment(), SettingsView {
var viewmodel :SettingsViewModel? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
viewmodel = ViewModelProviders.of(this).get(SettingsViewModel(this)::class.java)
val binding =
DataBindingUtil.inflate<FragmentSettingsBinding>(inflater, R.layout.fragment_settings, container, false)
.apply{}
return binding.root
}