如何修复实例问题依赖注入dagger2

时间:2019-02-07 18:01:21

标签: java android dependency-injection dagger-2

我需要使实例脱离我的对象。我为此使用了Dagger 2。

为此,我做到了:

public class OfflineCommonData {

private MainActivityViewModel mainActivityViewModel;

public OfflineCommonData(Object viewModel){
    if(viewModel instanceof MainActivityViewModel)
        mainActivityViewModel = (MainActivityViewModel) viewModel;
}

public MainActivityViewModel getMainActivityViewModel() {
    return mainActivityViewModel;
}

public void setMainActivityViewModel(MainActivityViewModel mainActivityViewModel) {
    this.mainActivityViewModel = mainActivityViewModel;
}
}

AppModule:

@Singleton @Provides
OfflineCommonData provideOfflineCommonData(Object viewModel){ return new OfflineCommonData(viewModel); }

最后我的接待:

@Inject
public OfflineDataViewModel(@NonNull Application application, @NonNull DataManager dataManager, @NonNull OfflineCommonData offlineCommonData) {
    super(application);
    this.dataManager = dataManager;
    this.offlineCommonData = offlineCommonData;

我知道了

Cannot resolve symbol 'DaggerAppComponent'.

有帮助吗?

0 个答案:

没有答案