Type mismatch: inferred type is but was expected
14,523
Solution 1
solved it by adding the following line to my gradle.properties:
android.databinding.enableV2=false
Solution 2
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.
Related videos on Youtube
Author by
MoeinDeveloper
Updated on June 04, 2022Comments
-
MoeinDeveloper almost 2 years
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?
-
Leonardo Velozo over 5 yearsCompiler was expecting
NoteViewModel?
but it's gettingNoteViewModel
(the difference is the?
that indicates it may be null) -
NIKHIL MAURYA over 5 yearsi think this code ViewModelProviders.of(this,viewModelFactory) .get(NoteViewModel::class.java) is returning a Nullable type
-
MoeinDeveloper over 5 years@NIKHILMAURYA nope! I think it's because of the new android studio. I had the same project before the update and it worked! but after the update I face this issue
-
NIKHIL MAURYA over 5 yearsMaybe it's a problem similar to the one I was facing here stackoverflow.com/questions/52550348/… I had to change the server response so to make it work at that time.
-
MoeinDeveloper over 5 years@NIKHILMAURYA Yeah I have this issue too!
-
-
Parth Anjaria over 5 yearsgetting this response : "The option setting 'android.databinding.enableV2=false' is experimental and unsupported. The current default is 'true'"
-
MoeinDeveloper over 5 yearsIt's just a warning, you can ignore it!
-
MoeinDeveloper over 4 yearsyes! I found out this too! after changing package names, I never faced that problem! weird!
-
Subhan Ali about 4 yearsMade my Day! <3
-
Akira Chen about 2 yearsI add the line definition, the Build console appears the error as below: A problem occurred evaluating project ':app'. > Failed to apply plugin [id 'com.android.internal.application'] > The option 'android.databinding.enableV2' is deprecated. The current default is 'true'. It has been removed from the current version of the Android Gradle plugin. Databinding v1 is removed.