【问题标题】:Fragment databinding, DataBinderMapperImpl.java cannot find symbol FragmentCollectionBindingImpl片段数据绑定,DataBinderMapperImpl.java 找不到符号 FragmentCollectionBindingImpl
【发布时间】:2019-05-24 16:02:02
【问题描述】:

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

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

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

这是我的项目 gradle buildscript

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'
 }
}

和应用程序分级

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 中

片段集合绑定

被创建

【问题讨论】:

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


    【解决方案1】:

    我遇到了同样的问题:

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

    【讨论】:

    • 太棒了!你为我节省了很多时间。我刚刚更新了一个回调函数,忘记更新 XML。
    • 这正是我的情况。 Button 的 onClick 方法中的拼写错误。非常感谢。
    【解决方案2】:

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

    之前的代码:

    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 的参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 2020-03-13
      相关资源
      最近更新 更多