Currently started to learn MVVM Kotlin. This youtube tutorial is pretty confusing but direct to the point.
https://www.youtube.com/watch?v=0LaUXQcGuT0&t=254s
I have this error in my title.
this code triggers the error in my MainActivity

So here's my Module:app (build.gradle)
Dependencies
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//Config data binding compiler with Kotlin
kapt 'com.android.databinding:compiler:3.1.1'
//LifeCycle Extensions
implementation 'android.arch.lifecycle:extensions:1.1.1'
//Toasty to show Toast
implementation 'com.github.GrenderG:Toasty:1.2.8'
}
DataBinding
dataBinding{
enabled=true
}
applied plugin
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.jetbrains.kotlin.kapt"
apply plugin: 'kotlin-kapt'
My activity_main.xml
<data>
<variable
name="viewModel"
type="com.example.rnd.kotlinmvvmlogin.ViewModel.LoginViewModel"/>
</data>
and Here's my File Structure of MVVM

MainActivity.kt
package com.example.rnd.kotlinmvvmlogin.View
import android.arch.lifecycle.ViewModelProviders
import android.databinding.DataBindingUtil
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.rnd.kotlinmvvmlogin.Interface.LoginResultCallbacks
import com.example.rnd.kotlinmvvmlogin.R
import com.example.rnd.kotlinmvvmlogin.ViewModel.LoginViewModel
import com.example.rnd.kotlinmvvmlogin.ViewModel.LoginViewModelFactory
import com.example.rnd.kotlinmvvmlogin.databinding.ActivityMainBinding
import es.dmoral.toasty.Toasty
class MainActivity : AppCompatActivity(), LoginResultCallbacks {
override fun onSuccess(message: String) {
Toasty.success(this, message, Toast.LENGTH_SHORT)
.show()
}
override fun onError(message: String) {
Toasty.error(this, message, Toast.LENGTH_SHORT)
.show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val activityMainBinding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
activityMainBinding.viewModel = ViewModelProviders.of(this, LoginViewModelFactory(this))
.get(LoginViewModel::class.java)
}
}
I understand the process of MVVM but I don't know what is reason why this error keeps occuring. The tutorial of this MVVM is really helpful for understanding the mvvm kotlin that's why I'm pursuing to continue this app to make it work.
I had same issue. Problem is in your type type="com.example.rnd.kotlinmvvmlogin.ViewModel.LoginViewModel"
Remember in databinding only Viewmodel class name should start with Capital letter and other parent folder of a class should start with lower case. Just rename ViewModelwith viewModel. Clear and Rebuild the project.
Many people experience this because they have their packages named starting with a capital letter. Rename your package names to start with a small case letters such that in your path of reference to view Model class its only the view Model class that starts with a capital letter
i.e type="com.example.rnd.kotlinmvvmlogin.viewModel.LoginViewModel"
solves your problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With