我目前正在试用新的ViewBindings,但我不会让它们开始工作。我上Android Studio 3.6.1了。我刚刚创建了一个新项目,并将viewBinding.enabled = true添加到模块build.gradle中。但是当我试图访问MainActivityBinding类时,它说它不能解析这个符号。Autocomplete没有找到任何类似于绑定类的东西。我也尝试了一个不同的项目使用Kotlin,但没有成功。AS4.0也帮不上忙。要生成ViewBinding类,我需要做什么?
我的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
viewBinding {
enabled = true
}
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 29
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'
}
}
}
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'
}发布于 2020-03-03 14:21:06
直到我意识到绑定是从XML文件("fragment_home.xml")而不是类("HomeFragment.kt")获取它们的名称,我才找到我的fragment_home.xml文件。所以我在HomeFragmentBinding找不到他们,但我在FragmentHomeBinding.找到他们
我发现将每个ViewBinding看作是作为该XML文件的委托创建的助手单例是很有帮助的。
(编辑以删除过时的分级材料)
发布于 2021-11-11 17:49:28
布局的根标记必须是<layout>。确保这不是你的案子。
必须更改我的原始布局XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello World!" />
</androidx.constraintlayout.widget.ConstraintLayout>至
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello World!" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>发布于 2022-01-06 13:44:29
在我的例子中,我试图绑定视图
添加build.gradle (模块)
buildFeatures{
dataBinding true
viewBinding true
}前一个是
viewBinding{
enabled = true
}https://stackoverflow.com/questions/60464962
复制相似问题