Android studio 片段数据绑定,DataBinderMapperImpl.java找不到符号FragmentCollectionBindingImpl
我正在kotlin中绑定android片段和ViewModel之间的数据。编译器抛出 错误:找不到symbolimport>frydzej.yerbet.databinding.FragmentCollectionBindingImpl 我尝试将gradle版本更改为3.2.1,将数据绑定编译器版本更改为3.2.1。但结果是一样的。 我的Android Studio版本是3.3 RC 3 这是我的gradle构建脚本项目Android studio 片段数据绑定,DataBinderMapperImpl.java找不到符号FragmentCollectionBindingImpl,android-studio,data-binding,kotlin,compiler-errors,Android Studio,Data Binding,Kotlin,Compiler Errors,我正在kotlin中绑定android片段和ViewModel之间的数据。编译器抛出 错误:找不到symbolimport>frydzej.yerbet.databinding.FragmentCollectionBindingImpl 我尝试将gradle版本更改为3.2.1,将数据绑定编译器版本更改为3.2.1。但结果是一样的。 我的Android Studio版本是3.3 RC 3 这是我的gradle构建脚本项目 buildscript { ext.kotlin_version = '1
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-rc03'
classpath "org.jetbrains.kotlin:kotlin-gradle-
plugin:$kotlin_version"
classpath 'android.arch.navigation:navigation-safe-args-gradle-
plugin:1.0.0-alpha09'
}
}
和应用程序gradle
dataBinding{
enabled = true
}
dependencies {
def lifecycle_version = "2.0.0"
def room_version = "2.0.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha09"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha09"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation 'com.cuneytayyildiz:onboarder:1.0.3'
implementation 'com.android.support:design:28.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
testImplementation 'junit:junit:4.12'
debugImplementation 'com.amitshekhar.android:debug-db:1.0.4'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
fragment_collection.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="viewModel" type="frydzej.yerbet.viewModels.CollectionViewModel"/>
</data>
<FrameLayout
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".Views.CollectionFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_item_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_add"
android:onClick="@{() -> viewModel.addFabClicked()}"
android:layout_margin="16dp"
app:fabSize="normal"/>
</FrameLayout>
</layout>
在MainActivity数据绑定中,有
活动主绑定
及
ActivityMainBindingImpl
但仅在CollectionFragment中
碎片收集绑定
创建了好的,我找到了解决方案。我的ViewModel构造函数有一个参数,我必须创建自定义ViewModelProviderFactory才能工作。这不是安卓工作室的错 前代码:
val viewModel: CollectionViewModel = ViewModelProviders.of(this).get(CollectionViewModel::class.java)
以下代码:
val viewModel: CollectionViewModel = ViewModelProviders.of(this, CollectionViewModelFactory(activity!!.application)).get(CollectionViewModel::class.java)
在哪里
activity!!.application
是我的ViewModel的参数我有同样的问题:
最终的解决方案是,我拼错了片段XML文件中的一个值
看看android:onclick参数,其中有一个额外的()是我错误地添加的
错误
<Button
android:id="@+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/end_game"
android:theme="@style/SkipButton"
android:onClick="@{() -> gameViewModel.onGameFinished()()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
对
<Button
android:id="@+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/end_game"
android:theme="@style/SkipButton"
android:onClick="@{() -> gameViewModel.onGameFinished()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
我会建议您检查片段的XML文件中是否存在任何拼写错误或拼写错误的变量名,并更正它们。这解决了我的问题。
这些错误只会在编译时被捕获,并指向生成的数据绑定文件,这使得调试变得非常困难。Awesome!你帮我节省了很多时间。我刚刚更新了一个回调函数,却忘了更新XML。这正是我的情况。按钮onClick方法中的拼写错误。非常感谢。这是正确的解决方案。
<Button
android:id="@+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/end_game"
android:theme="@style/SkipButton"
android:onClick="@{() -> gameViewModel.onGameFinished()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />