error: cannot find symbol class ActivityMainBindingImpl

error: cannot find symbol class ActivityMainBindingImpl

我正在学习数据绑定,我现在试了 3 次,我完全按照教程做的,但是当我 运行 应用程序,

它生成一个 java 文件 ActivityMainBindingImpl,我的 SDK 应该有问题吗?

主要 :

  private ActivityMainBinding activityMainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        activityMainBinding.setPerson(getCurrentPerson());
    }

    public Person getCurrentPerson(){
        Person person = new Person();
        person.setName("lance");
        person.setLastName("claudio");
        return person;
    }

型号:

public class Person {
   private String name;

    public Person(String name, String lastName) {
        this.name = name;
        this.lastName = lastName;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    private String lastName;
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="person"
            type="com.pedrovs.databindingexample.Person" />

    </data>
    <LinearLayout 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"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{person.setName()}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{person.setLastName()}">

        </TextView>

    </LinearLayout>

</layout>

这是我的Gradle:APP

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    dataBinding{
        enabled=true
    }

    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.pedrovs.databindingexample"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

在 Gradle 中也启用了数据绑定。

谢谢!

使用这个 xml:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="person"
            type="com.pedrovs.databindingexample.Person" />

    </data>
    <LinearLayout 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"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{person.name}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{person.lastName}">

        </TextView>

    </LinearLayout>

</layout>

您在 xml 中使用数据绑定时出错,检查我的代码与您在 xml 中使用 person 的所有代码。