我正在尝试在我的项目中使用数据绑定库,这是用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()
无效并显示以下错误:Cannot access class 'com.xxx.ui.authorization.AuthorizationPresenter'. Check your module classpath for missing or conflicting dependencies
。
我尝试将kapt 'com.google.guava:guava:19.0'
添加到build.gradle
,但没有任何变化。
另外值得一提的是,只有当我在Kotlin中编写片段时才出现问题,当它用Java编写时,一切正常。
答案 0 :(得分:1)
在IDE显示错误之前,您可以使用:
binding.setVariable(BR.model, Model())
它不像kotlin形式那么简洁,但红色不会让你疯狂;)