Can't fix error: Cannot find symbol class ActivityMainBindingImpl

Can't fix error: Cannot find symbol class ActivityMainBindingImpl

尝试使用 MVVM 和数据绑定构建项目时出现以下错误。我已经搜索了我在 Whosebug 上可以找到的所有内容,或者搜索了互联网上的常见错误,但对我的情况没有任何帮助。
我收到的唯一消息是这个错误。在buildOutput中看起来像这样:

我的包用大写字母命名,我发现这可能是原因,因为编译器将它们视为 classes 的名称,所以我将它们全部更改为开始有小写字母,但这没有帮助。

我已经创建了 ViewModelFactory 用于在 Activity 中创建我的 ViewModel 这样我就可以使用工厂通过构造函数发送额外的参数,所以我尝试删除它并且不使用任何参数并创建没有实例的实例为此目的使用工厂,但我仍然没有得到任何结果(同样的错误)

我用不同的方式改变了两个 build.gradle,但结果总是一样的。
最后我从 XML 中删除了数据绑定和变量,然后我能够 运行 应用程序(有其他错误,但我可能能够自己处理这些错误)但我想保持原样,只处理我的错误。

我对 MVVM 和数据绑定没有经验,所以这可能只是一个愚蠢的错误,但如果我不知道我应该在哪里寻找它,就很难找到它。

这里我 post 最重要的代码,如果您需要更多,请告诉我:

build.grale(应用程序)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.rickmorty"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures{
        dataBinding = true
        viewBinding = true
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.1'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.3.0-alpha02'
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "androidx.cardview:cardview:1.0.0"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.7'

    implementation 'com.github.bumptech.glide:glide:4.6.1'
    kapt 'com.github.bumptech.glide:compiler:4.4.0'
}

**build.gradle(项目)

buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

MainActivity

class MainActivity : AppCompatActivity() {

    lateinit var mainViewModel: MainViewModel
    lateinit var mAdapter: CharactersAdapter
    lateinit var api: CharacterAPI

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val retrofit = RetrofitClient.instance
        api = retrofit.create(CharacterAPI::class.java)
        setupViewModel()
        setupRecycler()

        mainViewModel.getData().observe(this,
            Observer<List<Results>> { t ->
                mAdapter = CharactersAdapter(this@MainActivity, t!!)
                rvCharacters.adapter = mAdapter
            })
    }

    fun setupRecycler() {
        val lManager = LinearLayoutManager(this@MainActivity)
        rvCharacters.apply {
            setHasFixedSize(true)
            layoutManager = lManager
        }
        rvCharacters.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val visibleItems = lManager.childCount
                val totalItems = lManager.itemCount
                val firstVisible = lManager.findFirstVisibleItemPosition()
                if (dy > 0) {
                    if (!mainViewModel.isLoading.value!! && (visibleItems + firstVisible) >= totalItems) {
                        mainViewModel.scrolledNext()
                    }
                } else {
                    if (!mainViewModel.isLoading.value!! && (totalItems - visibleItems) <= 0) {
                        mainViewModel.scrolledPrev()
                    }
                }
            }

        })
    }

    fun setupViewModel() {
        mainViewModel = ViewModelProviders.of(this, MainViewModelFactory(application, api))
            .get(MainViewModel::class.java)
        DataBindingUtil.setContentView<ActivityMainBinding>(
            this, R.layout.activity_main
        ).apply {
            lifecycleOwner = this@MainActivity
            viewmodel = mainViewModel
        }
    }
}

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="com.example.rickmorty.ViewModel.MainViewModel"/>
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <RelativeLayout
            android:id="@+id/rlPageTitleHolder"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHeight_percent="0.1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:layout_centerInParent="true"
                android:text="@{() -> viewmodel.pageNumber}"/>
        </RelativeLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvCharacters"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/rlPageTitleHolder"
            app:layout_constraintHeight_percent="0.9"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

正如我之前所写,从 XML 中删除 variable 并从 MainActivity 中绑定后,错误消失了。 我知道这是很多代码,所以如果有多余的地方,请告诉我。仍然缺少工厂 class,但如果需要,我会 post。另外 MainRepo 是一个我没有附在这里的,但是它很长,但是如果你需要它我可以 post 全部。

这里是问题 --> android:text="@{() -> viewmodel.pageNumber}"

使用 dataBinding 赋值的正确语法是

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_centerInParent="true"
        android:text="@{viewmodel.pageNumber}"/>