无法在Android中解析符号'DataBindingUtil'?

问题描述 投票:0回答:1

我想使用Java中的DataBindingUtil类,但无法导入它。我的项目中似乎没有DataBindingUtil类,甚至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'
}
android android-databinding
1个回答
0
投票

您应该启用dataBinding而不是viewBinding尝试此操作

android {
    ...
    dataBinding {
        enabled = true
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.