错误:
error: [Dagger/MissingBinding] java.util.ArrayList<java.lang.Integer> cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_LifecycleComponentBuilderEntryPoint,
javax.inject.Provider<java.util.ArrayList<java.lang.Integer>> is injected at
space.rodionov.swedishdriller.DrillerViewModel_AssistedFactory(�, drillerCatList, �)
space.rodionov.swedishdriller.DrillerViewModel_AssistedFactory is injected at
space.rodionov.swedishdriller.DrillerViewModel_HiltModule.bind(factory)
java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>> is injected at
androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule.provideFactory(�, viewModelFactories)
@dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory java.util.Set<androidx.lifecycle.ViewModelProvider.Factory> is requested at
dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.ActivityEntryPoint.getActivityViewModelFactory() [space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.SingletonC ? space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.ActivityRetainedC ? space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.ActivityC]
The following other entry points also depend on it:
dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.FragmentEntryPoint.getFragmentViewModelFactory() [space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.SingletonC ? space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.ActivityRetainedC ? space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.ActivityC ? space.rodionov.swedishdriller.SwedishDrillerApplication_HiltComponents.FragmentC][WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).
在构建项目期间尝试启动它时发生此错误。 它发生在我完成 ViewModel 代码并从 Fragment 调用它的方法之后。 在我看来,我在 ViewModel 中犯了错误,但 Hilt 对我来说也是新的,也是 Kotlin 语言。 感谢您的帮助。 代码:
在片段中:
@AndroidEntryPoint
class DrillerFragment : Fragment(R.layout.cardstack_layout), CardStackListener {
private val viewModel: DrillerViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = CardstackLayoutBinding.bind(view)
val drillerLayoutManager = CardStackLayoutManager(requireContext(), this) // ЧТО ПИСАТЬ В CONTEXT??
val drillerAdapter = DrillerAdapter()
binding.apply {
cardStackView.apply {
adapter = drillerAdapter
layoutManager = drillerLayoutManager
itemAnimator = null
}
}
viewModel.getLivedataList().observe(viewLifecycleOwner) {
drillerAdapter.submitList(it)
}
viewModel.get4words()
}
在视图模型中:
class DrillerViewModel @ViewModelInject constructor(
private val wordDao: WordDao,
val drillerCatList: ArrayList<Int> = ArrayList(),
val mLivedataList: MutableLiveData<List<Word>>
) : ViewModel() {
val shownCategoriesFlow = wordDao.getShownCategoriesNumbers()
fun getLivedataList(): LiveData<List<Word>> {
return mLivedataList
}
fun get4words() = viewModelScope.launch {
shownCategoriesFlow.collect {
it.forEach { catNumber ->
drillerCatList.add(catNumber)
}
}
wordDao.get4words(drillerCatList).collect {
mLivedataList.value = it
}
}
}
答案 0 :(得分:1)
在构造函数中创建你的变量
val drillerCatList: ArrayList<Int> = ArrayList(),
val mLivedataList: MutableLiveData<List<Word>>
如果不通过 Hilt 注入,则作为类中的普通变量