片段数据绑定,DataBinderMapperImpl.java找不到符号FragmentCollectionBindingImpl

时间:2018-12-26 11:08:32

标签: android-studio data-binding kotlin compiler-errors

我在android片段和Kotlin中的ViewModel之间绑定数据。编译器抛出

  

错误:找不到符号导入> frydzej.yerbet.databinding.FragmentCollectionBindingImpl

我试图将gradle版本更改为3.2.1,并将数据绑定编译器版本更改为3.2.1。但结果相同。 我的Android Studio版本是3.3 RC 3

这是我的项目gradle构建脚本

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>

和片段视图代码:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val binding: FragmentCollectionBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_collection, container, false)
    val view: View = binding.root
    val viewModel: CollectionViewModel = ViewModelProviders.of(this).get(CollectionViewModel::class.java)
    binding.viewModel = viewModel
    return view
}

MainActivity数据绑定中有

  

ActivityMainBinding   和   ActivityMainBindingImpl,

但仅在CollectionFragment中

  

FragmentCollectionBinding

已创建

2 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。我的ViewModel构造函数只有一个参数,我必须创建自定义ViewModelProviderFactory才能工作。这不是Android Studio的问题。

之前的代码:

 Set<Double> priceOfProducts = new HashSet<Double>(); 

后面的代码:

val viewModel: CollectionViewModel = ViewModelProviders.of(this).get(CollectionViewModel::class.java)

其中

val viewModel: CollectionViewModel = ViewModelProviders.of(this, CollectionViewModelFactory(activity!!.application)).get(CollectionViewModel::class.java)

是我的ViewModel的参数

答案 1 :(得分:0)

我遇到了同样的问题:

解决方案最终是我拼写了片段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文件中是否有任何拼写错误或拼写错误的变量名,然后更正它们。这解决了我的问题。 这些错误只会在编译时被捕获,并指向生成的数据绑定文件,因此很难调试。