我正在将匕首与viewmodel
集成在一起。当我将viewmodelprovider
工厂包括在AppComponent
中时,应用程序无法构建,并且显示类似
“错误:[Dagger / MissingBinding]如果没有@Provides注释的方法,则无法提供java.util.Map,javax.inject.Provider>”。我在下面添加了我的代码。
ViewModelFactory
class DaggerViewModelFactory
@Inject
constructor(private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator: Provider<out ViewModel>? = creators[modelClass]
if (creator == null) {
for ((key, value) in creators) {
if (modelClass.isAssignableFrom(key)) {
creator = value
break
}
}
}
if (creator == null) {
throw IllegalArgumentException("unknown model class " + modelClass)
}
try {
return creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
然后我有我的 AppComponent 类
@Singleton
@Component(modules = [AndroidInjectionModule::class, ViewModelFactoryModule::class, ActivityBuilder::class])
interface AppComponent : AndroidInjector<FreedomApplication> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: DaggerApplication): Builder
fun build(): AppComponent
}
}
我的 viewmodel 模块绑定类
@Module
abstract class WelcomeModule {
@Binds
@IntoMap
@ViewModelKey(WelcomeViewModel::class)
abstract fun bindWelcomeViewModel(welcomeViewModel: WelcomeViewModel): ViewModel
}
我的片段生成器类
@Module
abstract class FragmentBuilder {
@ContributesAndroidInjector
abstract fun injectWelcomeFragment() : WelcomeFragment
}
然后我的片段类包含 viewmodelprovider 工厂
class WelcomeFragment @Inject constructor(): DaggerFragment() {
@Inject
lateinit var providerFactory: ViewModelProvider.Factory
companion object {
fun newInstance() = WelcomeFragment()
}
private lateinit var viewModel: WelcomeViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.welcome_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this, providerFactory).get(WelcomeViewModel::class.java)
// TODO: Use the ViewModel
}
}
和用于整合片段的视图模型
class WelcomeViewModel @Inject constructor() : ViewModel() {
// TODO: Implement the ViewModel
}
答案 0 :(得分:0)
解决了以上问题,请参考以下代码。
@Singleton
@Component(modules = [ActivityBuilder::class, NetworkModule::class, ViewBindModule::class, AndroidSupportInjectionModule::class])
interface AppComponent : AndroidInjector<FreedomApplication> {
override fun inject(application: FreedomApplication)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: FreedomApplication): Builder
fun build(): AppComponent
}
}
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [FragmentBuilder::class])
abstract fun bindWelcomeActivity (): WelcomeActivity
@ContributesAndroidInjector(modules = [FragmentBuilder::class])
abstract fun bindDashBoardActivity (): DashBoardActivity
}
@Module
abstract class ViewBindModule {
@Binds
abstract fun bindViewModelFactory(factory: DaggerViewModelFactory): ViewModelProvider.Factory
@Binds
@IntoMap
@ViewModelKey(WelcomeViewModel::class)
abstract fun bindWelcomeViewModel(viewModel: WelcomeViewModel): ViewModel
@Binds
@IntoMap
@ViewModelKey(LoginViewModel::class)
abstract fun bindLoginViewModel(viewModel: LoginViewModel): ViewModel
@Binds
@IntoMap
@ViewModelKey(DashBoardViewModel::class)
abstract fun bindDashboardViewModel(viewModel: DashBoardViewModel): ViewModel
}
class DaggerViewModelFactory @Inject constructor(private val creators: Map<Class <out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>):ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
var creator :Provider<ViewModel>? = creators[modelClass]
if (creator == null)
for ((key, value) in creators){
if (modelClass.isAssignableFrom(key)){
creator = value
break
}
}
if (creator == null) throw IllegalArgumentException("Unknown model class")
try{
return creator.get() as T
}catch (e:Exception){
throw RuntimeException(e)
}
}
}
abstract class FragmentBuilder {
@ContributesAndroidInjector
abstract fun bindWelcomeFragment(): WelcomeFragment
@ContributesAndroidInjector
abstract fun bindLoginFragment(): LoginFragment
@ContributesAndroidInjector
abstract fun bindDashBoardFragment(): DashBoardFragment
}