无法解析 android 中的符号 'DataBindingUtil'?

Can not resolve symbol 'DataBindingUtil' in android?

我想在 java 中使用 DataBindingUtil class,但我无法导入它。在我的项目中似乎没有 DataBindingUtil class 甚至 android.databinding.DataBindingUtil。我试过重新安装,但它没有解决我的问题。这是我的代码:

import android.databinding.DataBindingUtil;//cannot import


public class DeviceFragment extends Fragment{

    private DeviceViewModel deviceViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        //cannot use it
        DeviceFragmentBinding binding = DataBindingUtil.setContentView(this, R.layout.device_fragment);

还有我的 gradle 文件(我启用了 viewBinding):

apply plugin: 'com.android.application'

android {
    viewBinding {
        enabled = true
    }

    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.aqiapp"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    //custom
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //custom
    implementation 'com.squareup.retrofit2:retrofit:2.8.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.8.2'
    implementation 'com.google.dagger:dagger:2.27'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
}

您应该启用 dataBinding 而不是 viewBinding 试试这个

android {
    ...
    dataBinding {
        enabled = true
    }
}