我正在尝试在我的Kotlin项目中使用数据绑定库。我使用的是Kotlin v.1.0.2和Gradle插件v.2.12以及绑定编译器。我的build.gradle配置如下:
android {
dataBinding {
enabled = true
}
}
kapt {
generateStubs = true
}
dependencies {
kapt "com.google.dagger:dagger-compiler:2.2"
provided "org.glassfish:javax.annotation:10.0-b28"
kapt "com.android.databinding:compiler:2.12"
}
在我的Fragment布局中,我声明了
<data>
块,并缩写了一些变量:<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<import type="com.xxx.ui.AuthorizationPresenter"/>
<import type="com.xxx.entity.AuthProvider"/>
<variable
name="model"
type="com.xxx.ui.authorization.Model"/>
<variable
name="presenter"
type="com.xxx.ui.authorization.AuthorizationPresenter"/>
</data>
<FrameLayout
... />
</layout>
接着在我的AuthorizationFragment.kt
类中,我设置了这些变量:
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentAuthorizationBinding.inflate(inflater)
binding?.presenter = presenter
binding?.model = Model()
binding?.addOnRebindCallback(object : OnRebindCallback<FragmentAuthorizationBinding>() {
override fun onPreBind(binding: FragmentAuthorizationBinding?): Boolean {
val sceneRoot = binding?.root as ViewGroup
TransitionManager.beginDelayedTransition(sceneRoot)
return true
}
})
return binding?.root
}
问题是,虽然这段代码可以成功编译并构建APK,但Android Studio标记了这些行。
binding?.presenter = presenter
binding?.model = Model()
当我将片段写成Kotlin时,会出现以下错误:无法访问类'com.xxx.ui.authorization.AuthorizationPresenter'。检查模块类路径以查看遗漏或冲突的依赖项。
我尝试在我的build.gradle
中添加kapt 'com.google.guava:guava:19.0'
,但仍然没有改变。
值得一提的是,当我用Java编写时,问题就不存在了。