Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 299
MoeinDeveloper Avatar asked Oct 18 '25 18:10

MoeinDeveloper


2 Answers

Even though this is getting old, I found out that the problem came from the fact that my packages started with an uppercase.

You should check yours.

I find it very weird and frustrating that package naming influences the compiler as much.

gl Lawnio

edit: to avoid misunderstanding, I had the exact same two errors you were having.

like image 196
Quentin vk Avatar answered Oct 20 '25 07:10

Quentin vk


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

android.databinding.enableV2=false
like image 44
MoeinDeveloper Avatar answered Oct 20 '25 09:10

MoeinDeveloper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!