【发布时间】:2020-10-06 18:27:34
【问题描述】:
我想将我的 ViewModel 内部片段与 FragmentComponent 依赖项一起使用,但我认为
@ViewModelInject 在 ActivityComponent 或 ApplicationCoinmponent 中提供 ViewModel 并且无法将我的 FragmentComponent 依赖项注入到 ViewModel
class XViewModel @ViewModelInject constructor(
private val xClass: XClass // this dependency provides in FragmentComponent
) : ViewModel() {
}
当我尝试构建项目时。使用KaptException 构建失败,并且刀柄编译器说我无法提供XClass。其中XClass 在FragmentComonent 中提供。
当我将XClass 提供程序方法移动到ActivityComponent 或ApplicationComponent 时,使用KaptException 构建不再失败。
编辑:
我阅读了 Hilt 生成的代码,我的猜测是正确的,Hilt 和@ViewModelInject 在ActivityRetainedComponent 中安装XViewModel 模块。我不知道为什么?
@Module
@InstallIn(ActivityRetainedComponent.class)
@OriginatingElement(
topLevelClass = XViewModel.class
)
public interface XViewModel_HiltModule
【问题讨论】:
-
请将错误日志添加到问题中
-
在您有
XClass的提供者的位置添加类。并且您的问题的标题和描述不包含相同的问题。请说清楚 -
@AliSh 刀柄错误:如果没有
@Inject或提供程序方法,则无法提供XClass。但是XClass在FragmentComponent中提供,当我将XClass提供程序移动到ActivityComponent或ApplicationComponent时,一切正常。
标签: android viewmodel dagger-hilt