简体   繁体   中英

Kotlin data-binding issue type missmatch

I am trying to implement data-binding in my small kotlin project in a android. But getting error when assign viewmodel to databinding.

I have three class in this small project 1. User - A data model class 2. ActivityMainViewModel - View Model class 3. MainActivity - Class that will implement the data-binding

But in MainActivity.kt i am getting error when set the viewmodel to binding.

User.kt

class User() {
    var emailId: String= ""
    var password: String= ""

    constructor(emailId: String, password: String) : this() {
        this.emailId = emailId
        this.password = password
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        var myviewmodel= MainActivityViewModel()
        val userObj = User("abc@gmail.com","123344")
        myviewmodel.setUser(userObj)

        binding.viewModel = myviewmodel
    }
}

MainActivityViewModel.kt

class MainActivityViewModel() : ViewModel() {
    val userObj = User()

    fun setUser(user: User){
        userObj.emailId = user.emailId
        userObj.password = user.password
    }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
        <data>
               <variable
                        name="viewModel"
                        type="testing.justrade.databindingissue.ViewModel.MainActivityViewModel"
                />
        </data>
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".View.MainActivity">
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hello World!"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toTopOf="parent"/>
        </LinearLayout>
</layout>

I am getting this error.

E:\--------------------------/ MainActivity.kt: (21, 17): Cannot access class 'ViewModel.MainActivityViewModel'. Check your module classpath for missing or conflicting dependencies

Type mismatch: inferred type is testing.justrade.databindingissue.ViewModel.MainActivityViewModel but ViewModel.MainActivityViewModel? was expected

I have attached the screen shot of error in blow link -

https://ibb.co/Y25dbTn

You must not name your packages with CamelCase as defined in the Java Naming Conventions .

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

Especially when using the Android Data Binding Library this is relevant. In your case ViewModel is interpreted as a class and therefore ViewModel.MainActivityViewModel as a static inner class of it.

You'd have to rename your package into viewmodel to make it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM