< android.arch.lifecycle.ViewModel>>没有@Provides注释的方法无法提供 [英] <android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method

查看:76
本文介绍了< android.arch.lifecycle.ViewModel>>没有@Provides注释的方法无法提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个像示例但我遇到此错误


错误:没有@Provides注释的方法,将无法提供java.util.Map,javax.inject.Provider。


我遵循了所有示例,这是我的代码



ViewModelFactory类



  @Singletonpublic类ViewModelFactory实现了ViewModelProvider.Factory {private final Map< Class< ;?扩展ViewModel>,Provider< ViewModel>> mCreators; @Inject ViewModelFactory(Map< Class< ;?扩展了ViewModel> ;,提供者< ViewModel>>创建者){mCreators = creators; } @NonNull @Override public< T扩展了ViewModel> T create(@NonNull Class< T> modelClass){提供者< ;?扩展ViewModel> creator = mCreators.get(modelClass); if(creator == null){for(Map.Entry< Class<?extends ViewModel> ;, Provider< ViewModel>> entry:mCreators .entrySet()){if(modelClass.isAssignableFrom(entry.getKey())){ creator = entry.getValue();打破; }}} if(creator == null){抛出新的IllegalArgumentException(未知模型类 + modelClass); }试试{返回(T)creator.get(); } catch(Exception e){抛出新的RuntimeException(e); }}}  



ViewModelModule类



  @Modulepublic抽象类ViewModelModule {@绑定抽象ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);}  



这是组成部分



< pre class = snippet-code-js lang-js prettyprint-override> @ Singleton @ Component(modules = {AppModule.class,ViewModelModule.class})公共接口MainComponent {void inject(Sdk sdk) ; void injectTestActivity(TestActivity testActivity);}



ps:此实现在android库不在应用程序项目中

解决方案

您需要使用Dagger 多重绑定。换句话说,绑定视图模型并使用 @IntoMap 多重绑定注释对其进行注释。在您发布的同一示例中,您可以找到它的示例此处。在示例中,他们创建了 ViewModelKey 批注,以指定Dagger可以从地图中检索视图模型的键(通常是视图模型的类)。 Dagger将在编译时创建地图,这就是为什么会出现错误-如果您未指定任何视图模型作为地图的一部分,则Dagger无法知道应实例化哪种类型。


I am trying to create a viewmodel module like in this example but I am having this error

error: java.util.Map,javax.inject.Provider> cannot be provided without an @Provides-annotated method.

I followed all the example, here are my codes

ViewModelFactory class

@Singleton
public class ViewModelFactory implements ViewModelProvider.Factory {

    private final Map<Class<? extends ViewModel>, Provider<ViewModel>> mCreators;

    @Inject
    ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
        mCreators = creators;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        Provider<? extends ViewModel> creator = mCreators.get(modelClass);
        if (creator == null) {
            for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : mCreators
                    .entrySet()) {
                if (modelClass.isAssignableFrom(entry.getKey())) {
                    creator = entry.getValue();
                    break;
                }
            }
        }
        if (creator == null) {
            throw new IllegalArgumentException("unknown model class " + modelClass);
        }
        try {
            return (T) creator.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

ViewModelModule class

@Module
public abstract class ViewModelModule {

    @Binds
    abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory factory);

}

and this is the component

@Singleton
@Component(modules = {AppModule.class, ViewModelModule.class})
public interface MainComponent {


    void inject(Sdk sdk);

    void injectTestActivity(TestActivity testActivity);


}

ps: this implementation in android library not in the application project

解决方案

You need to bind your view models using Dagger multibindings. In other words, bind your view models and annotate them with the @IntoMap multibinding annotation. In the same example you posted, you can find an example of it here. In the example, they created the ViewModelKey annotation in order to specify the key from which Dagger can retrieve your view model from the map (usually the view model's class). Dagger will create the map at compile time, and that's why you get the error - if you don't specify any view model to be part of the map, Dagger can't know which types it should instantiate.

这篇关于&lt; android.arch.lifecycle.ViewModel&gt;&gt;没有@Provides注释的方法无法提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆