无法使用Dagger2 Android Java注入ViewModel

时间:2019-01-10 09:18:26

标签: java android mvvm dagger-2 android-architecture-components

我试图借助 dagger2 中的常见 ViewModelProviderFactory 注入 viewmodel 。 (这是我第一个使用匕首和architecure组件的应用程序)这是我在构建代码时遇到的错误。

  

错误:(26,10)错误:如果没有@Provides批注的方法,则无法提供java.util.Map,javax.inject.Provider>。

enter image description here

这是我使用的代码

ViewModelProviderFactory

@PerActivity

public class ViewModelProviderFactory implements ViewModelProvider.Factory {

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

    @Inject
    public ViewModelProviderFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
        this.creators = creators;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        Provider<? extends ViewModel> creator = creators.get(modelClass);
        if (creator == null) {
            for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.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

@Module
public abstract class ViewModelModule {
    @Binds
    @IntoMap
    @ViewModelKey(HomeViewModel.class)
    abstract ViewModel bindHomeViewModel(HomeViewModel homeViewModel);

    @Binds
    abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelProviderFactory factory);
}

ApplicationComponent

@Singleton
@Component(modules = {ApplicationModule.class, ViewModelModule.class})
public interface ApplicationComponent {

    void inject(BaseApp app);

    @ApplicationContext
    Context getContext();

    Application getApplication();

    WebService getWebService();

    PreferencesHandler getPreferencesHandler();

    InternetUtils getInternetUtils();
}

ActivityComponent

@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {

    void inject(MainActivity mainActivity);

    void inject(SampleListActivity sampleListActivity);

    void inject(HomeActivity homeActivity);
}

ViewModelKey

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
public @interface ViewModelKey {
    Class<? extends ViewModel> value();
}

在我的活动中,我使用此代码

@Inject
ViewModelProviderFactory mViewModelFactory;
........
oncreate(){
...
homeViewModel = ViewModelProviders.of(this,mViewModelFactory).get(HomeViewModel.class);
.......
}

1 个答案:

答案 0 :(得分:2)

您的ActivityComponentComponent的{​​{1}}(不是子组件),因此它无权访问AppComponent中设置的Map。

要么切换为ApplicationComponent使用SubComponent,要么确保在AppComponent中包括ActivityComponent的配置方法,以便从属组件可以访问它。

Map<Class<? extends ViewModel>, Provider<ViewModel>>