突然我开始得到这个错误,我不知道为什么如果有人告诉我这个错误在哪里,就足够有帮助了。正如我所能得到的,这是因为android studio的新更新。 我得到的错误的详细总结。

Task :app:kaptDebugKotlin
    ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1C:\Users\shubh\Downloads\MarginCalculator\app\build\generated\source\kapt\debug\com\kotlin_developer\margincalculator\DataBinderMapperImpl.java:10: error: cannot find symbol
    import com.kotlin_developer.margincalculator.databinding.FragmentCalculatorScreenBindingImpl;

    symbol:   class FragmentCalculatorScreenBindingImpl

    Task :app:kaptDebugKotlin FAILED
    location: package com.kotlin_developer.margincalculator.databinding
    FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 17s
29 actionable tasks: 27 executed, 2 up-to-date

当前回答

在我的情况下,有旧的依赖需要升级, 所以你可以这样做:

1.文件->项目结构->

2.进入建议->

3.在警告部分,Android工作室会告诉你哪些依赖 需要升级->

4.只需从那里直接升级它->

5.单击APPLY - - - >

现在就可以了!

谢谢!

其他回答

我也有同样的问题。让我带你们看一下我是如何解决这个问题的,以及我是如何解决它的,也许你们会有更大的了解。

之前解决

@Entity(tableName = "modules")
data class Module
(
    @PrimaryKey val id: Int,
    val name: String
)

@Entity(tableName = "sessions")
data class Session
(
    @PrimaryKey(autoGenerate = true) var id: Int,
    @ColumnInfo(name = "module_id") val moduleId: Int,
    @ColumnInfo(name = "start_time") val startTime: String,
    @ColumnInfo(name = "end_time") val endTime: String
)

data class ModuleSession
(
    @Embedded val module: Module,
    @Relation(
        parentColumn = "id",
        entityColumn = "module_id"
    )
    val sessions: List<Session>,
    @ColumnInfo(name = "is_updated") val isUpdated: Boolean = false // The problem
)

在DAO中

@Transaction
@Query("SELECT * FROM modules")
abstract suspend fun getModuleSession(): List<ModuleSession>

我得到的错误是

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

所以我深入挖掘,找到了下面的信息

The columns returned by the query does not have the fields [isUpdated] in com.gmanix.oncampusprototype.Persistence.ModuleSession even though they are annotated as non-null or primitive. Columns returned by the query: [id,name]
    public abstract java.lang.Object getModuleSession(@org.jetbrains.annotations.NotNull()

我从POJO ModuleSession中删除了字段IsUpdated,并将其添加到会话表中

后的变化

@Entity(tableName = "sessions")
data class Session
(
    @PrimaryKey(autoGenerate = true) var id: Int,
    @ColumnInfo(name = "module_id") val moduleId: Int,
    @ColumnInfo(name = "start_time") val startTime: String,
    @ColumnInfo(name = "end_time") val endTime: String,
    @ColumnInfo(name = "is_updated") val isUpdated: Boolean = false
)

data class ModuleSession
(
    @Embedded val module: Module,
    @Relation(
        parentColumn = "id",
        entityColumn = "module_id"
    )
    val sessions: List<Session>
)

另一方面,交叉检查SELECT语句中是否有可能导致问题的字段,或者您可以使用@Ignore对其进行注释

但是,如果你仍然觉得不舒服,你可以发布你的代码。

我希望这能有所帮助

在我的例子中,这是因为我没有在我的ViewModel中实现Observable。我添加了一个EditText约束布局与android:text="@={addProductViewModel.inputProductName}"

一旦我在ViewModel类中实现了Observable,错误就消失了

ViewModel

class AddProductViewModel (
    private val repository: ProductRepository,
    private val context: Context
): ViewModel(), Observable {

    @Bindable
    val inputProductName = MutableLiveData<String>()


    fun addProduct() {
        //inputProductName.value
    }

    override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }

    override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }
}

使用片段的MVVM数据绑定的完整示例

布局- add_product.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <data class=".AddProductBinding">
        <variable
            name="addProductViewModel"
            type="com.rao.iremind.AddProductViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <EditText
            android:id="@+id/editTextTextProductName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Product name"
            android:inputType="textPersonName"
            android:text="@={addProductViewModel.inputProductName}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />



    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

AddProductFragment

class AddProductFragment: Fragment() {
    private lateinit var binding: AddProductBinding
    private lateinit var addProductViewModel: AddProductViewModel
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding =  DataBindingUtil.inflate(inflater, R.layout.add_product, container, false)
        val dao = SubscriberDatabase.getInstance(requireActivity().applicationContext).productDAO
        val repository = ProductRepository(dao)
        val factory = AddProductViewModelFactory(repository, requireActivity().applicationContext)
        addProductViewModel = ViewModelProvider(this, factory).get(AddProductViewModel::class.java)
        binding.addProductViewModel = addProductViewModel
        binding.lifecycleOwner = this
        val view = binding.root

        return view
    }
}

添加产品视图模型

class AddProductViewModel (
    private val repository: ProductRepository,
    private val context: Context
): ViewModel(), Observable {

    @Bindable
    val inputProductName = MutableLiveData<String>()


    fun addProduct() {
        //inputProductName.value
    }

    override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }

    override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }
}

希望这能有所帮助 R

对我来说,这个问题的发生是因为java版本的差异 对我来说 -我的工作室默认java版本是11 -我有一个库在我的项目与java版本8支持

修复, 1.使用命令/usr/libexec/java_home -V列出所有已安装的java版本 2.拷贝java8路径:/Library/Java/JavaVirtualMachines/1.8.0_232.jdk/Contents/Home 3.去 文件-项目结构- SDK位置- jdk位置 在那里添加你的java路径

此错误也是由于数据绑定错误造成的

检查变量是否指向正确的数据类 检查视图的字段(文本视图、可见性是否正确) 您已经导入了正确的导入(例如与可见性相关的操作)

在我的情况下,有旧的依赖需要升级, 所以你可以这样做:

1.文件->项目结构->

2.进入建议->

3.在警告部分,Android工作室会告诉你哪些依赖 需要升级->

4.只需从那里直接升级它->

5.单击APPLY - - - >

现在就可以了!

谢谢!