Search code examples
androidandroid-studiokotlindagger-2android-databinding

Type mismatch: inferred type is but was expected


I'm new to Kotlin and here is my problem:

I use dataBinding in my app and When I want to set my variable from layout:

<?xml version="1.0" encoding="utf-8"?><layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <import type="android.view.View"/>
    <variable
            name="noteViewModel"
            type="moeindeveloper.kotlinroomaac.ViewModel.NoteViewModel" />
</data>

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".View.MainActivity">
    <ProgressBar
            android:id="@+id/loading"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:indeterminate="true"
            android:visibility="@{noteViewModel.isLoading.get() ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="@id/repository_rv"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="@id/repository_rv"
            app:layout_constraintTop_toTopOf="@id/repository_rv" />

    <android.support.v7.widget.RecyclerView
            android:id="@+id/repository_rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminate="true"
            android:visibility="@{noteViewModel.isLoading.get() ? View.GONE : View.VISIBLE}"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/row_item" />
    <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/addButton"
            android:src="@drawable/ic_add"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.98"/>
</android.support.constraint.ConstraintLayout>

My main activity:

@Inject lateinit var viewModelFactory: ViewModelProvider.Factory

on create:

val viewModel:NoteViewModel = ViewModelProviders.of(this,viewModelFactory)
        .get(NoteViewModel::class.java)

    binding.noteViewModel = viewModel
    binding.executePendingBindings()

so up to this point, my code is correct but, when I hit run, I face these errors:

Cannot access class 'ViewModel.NoteViewModel'. Check your module classpath for missing or conflicting dependencies

Type mismatch: inferred type is moeindeveloper.kotlinroomaac.ViewModel.NoteViewModel but ViewModel.NoteViewModel? was expected

I use android studio 3.2.1 kotlin version: 1.2.71

any thoughts on this?


Solution

  • solved it by adding the following line to my gradle.properties:

    android.databinding.enableV2=false