gpt4 book ai didi

java - 无法使用 dagger2 android java 注入(inject) ViewModel

转载 作者:搜寻专家 更新时间:2023-11-01 08:18:59 25 4
gpt4 key购买 nike

我正在尝试借助 dagger2 中常见的 ViewModelProviderFactory 注入(inject) viewmodel。 (这是我的第一个带有 Dagger 和架构组件的应用程序)这是我在构建代码时遇到的错误。

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

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);
}

应用组件

@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();
}

Activity 组件

@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();
}

在我的 Activity 中我使用这段代码

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

最佳答案

你的 ActivityComponentComponent (不是子组件)依赖于 ApplicationComponent , 因此它无法访问在 AppComponent 中设置的 map 。

要么切换到使用 SubComponent为你的ActivityComponent ,或确保包含 Map<Class<? extends ViewModel>, Provider<ViewModel>> 的提供方法在您的 AppComponent 中,以便依赖组件可以访问它。

interface AppComponent {
// ..
Map<Class<? extends ViewModel>, Provider<ViewModel>> creators();
}

关于java - 无法使用 dagger2 android java 注入(inject) ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54125354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com