[英]Data Binding Kotlin Android studio errors
I am trying to use Data binding library in my project, that is written in Kotlin. 我正在尝试在我的项目中使用数据绑定库,这是用Kotlin编写的。 I am using Kotlin v.1.0.2 & Gradle plugin v. 2.12 and with binding compiler.
我正在使用Kotlin v.1.0.2和Gradle插件v.2.12以及绑定编译器。 My build.gradle configured as follows:
我的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"
}
In my Fragment layout I declared <data>
block with some variables: 在我的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>
And then in my AuthorizationFragment.kt
class I am setting these variables: 然后在我的
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
}
And the problem is though this code successfully compiles and builds APK, Android studio marks these lines 问题是虽然这段代码成功编译并构建了APK,Android studio标记了这些行
binding?.presenter = presenter
binding?.model = Model()
as invalid and shows the following error: Cannot access class 'com.xxx.ui.authorization.AuthorizationPresenter'. Check your module classpath for missing or conflicting dependencies
为无效并显示以下错误:
Cannot access class 'com.xxx.ui.authorization.AuthorizationPresenter'. Check your module classpath for missing or conflicting dependencies
Cannot access class 'com.xxx.ui.authorization.AuthorizationPresenter'. Check your module classpath for missing or conflicting dependencies
. Cannot access class 'com.xxx.ui.authorization.AuthorizationPresenter'. Check your module classpath for missing or conflicting dependencies
。 I tried to add kapt 'com.google.guava:guava:19.0'
to my build.gradle
, but nothing has changed. 我尝试将
kapt 'com.google.guava:guava:19.0'
到我的build.gradle
,但没有任何改变。 Also worth to mention that problem is only appears when I write my fragment in Kotlin, when it is written in Java everything is ok. 另外值得一提的是,只有当我在Kotlin中编写片段时才出现问题,当它用Java编写时,一切正常。
Until the IDE shows the error, you can use this: 在IDE显示错误之前,您可以使用:
binding.setVariable(BR.model, Model())
It is not so concise as kotlin form but the red color will not drive you crazy any more ;) 它不像kotlin形式那么简洁,但红色不会让你疯狂;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.