无法访问活动绑定Android [英] Cannot access activity binding android
问题描述
我有一个名为activity_suggestions
的布局文件.我在其中使用数据绑定.因此,生成了文件ActivitySuggestionsBinding
.该项目成功编译.但是当我尝试运行项目时,出现此错误
I have a layout file called activity_suggestions
. I am using databinding in it. Hence the file ActivitySuggestionsBinding
got generated. The project compiles successfully. But when I try to run the project, I get this error
e: error: cannot access ActivitySuggestionsBinding
我正在使用Kotlin版本1.4.1
的android studio 3.1.2
.任何帮助将不胜感激
I am using android studio 3.1.2
with kotlin version 1.4.1
. Any help will be appreciated
修改
粘贴我的模块级别的build.gradle和应用程序级别的build.gradle
Edit
Pasting my module level build.gradle and app level build.gradle
模块Build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled = true
}
..
}
dependencies{
..
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
provided 'javax.annotation:jsr250-api:1.0'
implementation "android.arch.lifecycle:runtime:$rootProject.archVersion"
implementation "android.arch.lifecycle:extensions:$rootProject.archVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
App build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android{
dataBinding{
enabled = true
}
..
}
dependencies{
compile project(':module')
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
这是我正在访问ActivitySuggestionsBinding
的活动.这样即可编译,没有任何错误.
This is the activity where I am accessing ActivitySuggestionsBinding
. This compiles without any error.
class SuggestionsActivityScreen : BaseActivity() {
var binding : ActivitySuggestionsBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_suggestions)
binding?.model = SuggestionActivityViewModel()
}
}
在编译基本模块(应用程序)时,这是我得到的错误
On compiling the base module (app), this is the error I get
error: cannot access ActivitySuggestionsBinding
class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
这是我的activity_suggestions.xml
This is my activity_suggestions.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.dom.domp.SuggestionActivityViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:padding="@dimen/step1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@{model.namedString}"/>
</RelativeLayout>
</layout>
我已经尝试过清理无效的缓存.这些不能解决问题.
And I have tried clean, invalidate cache. These don't solve the problem.
推荐答案
我遇到了类似的问题. 我遇到了错误
I had similar issue. I was getting error
error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found
花了几天的时间才发现问题出在GDB上,而不是Dagger2上.事实证明,Dagger不支持在其生成的代码中使用生成的类.
Spent a couple of days to figure out that the problem is not with GDB but with Dagger2. It turned out, Dagger doesn't support using generated classes in code it generates.
所以,我的:library
模块中有这样的类:
So, I had such class in my :library
module:
class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}
和它的匕首组件位于:app
模块中.因此,这两个注释处理器会相互干扰.
And the dagger component for it were located in :app
module. So the two annotation processors interfere each other.
从片段中删除泛型后,它可以正常编译.
After I removed generic from my fragments it compiles fine.
因此,如果您遇到此类错误,请尝试查看是否尝试注入(或提供)由另一个注释处理程序生成的类,或从此类注释继承的类,或者该类具有泛型的类.
So if you have such error, try to look if you try to inject into (or provide) class which is generated by another annotation proccessor, or inherits from such class, or have generic with such class.
这篇关于无法访问活动绑定Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!