当前位置: 首页 > 知识库问答 >
问题:

类型不匹配:推断类型是,但应是

荀嘉熙
2023-03-14

我对Kotlin是新来的,这是我的问题:

<?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>
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
val viewModel:NoteViewModel = ViewModelProviders.of(this,viewModelFactory)
        .get(NoteViewModel::class.java)

    binding.noteViewModel = viewModel
    binding.executePendingBindings()
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

我使用android studio 3.2.1 kotlin版本:1.2.71

对此有什么想法吗?

共有1个答案

卓宏达
2023-03-14

尽管这已经过时了,但我发现问题来自于我的包以大写字母开头的事实。

你应该检查一下你的。

我发现包命名对编译器的影响如此之大,这非常奇怪和令人沮丧。

格尔拉夫尼奥

编辑:为了避免误解,我有两个与你完全相同的错误。

 类似资料: