Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法使用Dagger2注入livedata存储库类_Android_Mvvm_Dagger 2_Android Livedata - Fatal编程技术网

Android 无法使用Dagger2注入livedata存储库类

Android 无法使用Dagger2注入livedata存储库类,android,mvvm,dagger-2,android-livedata,Android,Mvvm,Dagger 2,Android Livedata,我有一个Android项目,它使用Dagger2、Livedata和viewmodel、rxjava和数据绑定 到目前为止,我已经创建了一个AppComponent和两个子组件。Appcomponent用于applicationscope,一个子组件ControllerComponent用于活动、片段和对话框。此ControllerComponent被注入baseactivity,并且工作正常 另一个子组件是用于viewmodel存储库的组件。现在,我似乎找不到将改装组件注入基本存储库的方法,因

我有一个Android项目,它使用Dagger2、Livedata和viewmodel、rxjava和数据绑定

到目前为止,我已经创建了一个AppComponent和两个子组件。Appcomponent用于applicationscope,一个子组件ControllerComponent用于活动、片段和对话框。此ControllerComponent被注入baseactivity,并且工作正常

另一个子组件是用于viewmodel存储库的组件。现在,我似乎找不到将改装组件注入基本存储库的方法,因为它是一个子组件,并且我在存储库类中没有applicationcontext

应用组件:

@ApplicationScope
@Component(modules = {
    ApplicationModule.class,
    PrefModule.class
})
public interface AppComponent {
  public void inject(AppInstance appInstance);

  public AppPreference getAppPref();

  public Application getApplication();

  public ControllerComponent newControllerComponent(ControllerModule controllerModule);

  public RetrofitComponent newRetrofitComponent(RetrofitModule retrofitModule);
}
@RepositoryScope
@Subcomponent(modules = {RetrofitModule.class})
public interface RetrofitComponent {
  public void inject(BaseRepo baseRepo);

  public APICall getApiCalls();
}
改装部件:

@ApplicationScope
@Component(modules = {
    ApplicationModule.class,
    PrefModule.class
})
public interface AppComponent {
  public void inject(AppInstance appInstance);

  public AppPreference getAppPref();

  public Application getApplication();

  public ControllerComponent newControllerComponent(ControllerModule controllerModule);

  public RetrofitComponent newRetrofitComponent(RetrofitModule retrofitModule);
}
@RepositoryScope
@Subcomponent(modules = {RetrofitModule.class})
public interface RetrofitComponent {
  public void inject(BaseRepo baseRepo);

  public APICall getApiCalls();
}
改装模块:

@Module
public class RetrofitModule {
  @Provides
  @RepositoryScope
  public APICall providePostApi(Retrofit retrofit) {
    return retrofit.create(APICall.class);
  }

  @Provides
  @RepositoryScope
  public Retrofit provideRetrofitInterface() {
    return new RetrofitService().retrofit;
  }
}
BaseRepo

@Inject
public APICall apiCall;

private Disposable subscription;
CompositeDisposable compositeDisposable = new CompositeDisposable();

private RetrofitComponent injector;

public BaseRepo() {
    //injector = DaggerRetrofitComponent.builder().retrofitModule(new RetrofitModule()).build();(I tried with dependent component had different problem)
    injector = ((AppInstance)getApplication()).getAppComponent().newRetrofitComponent(new RetrofitModule());(Not able to use getApplication())
    injector.inject(this);
}
我将使用子存储库从其相应的viewmodels扩展此基础存储库,因此我不打算从活动或片段传递应用程序

我可能错过了什么?还是做错了?还是我的方法错了


完成后还有一件事,我应该在Viewmodel类中注入存储库类,还是创建一个factory类并调用我需要的存储库,这是更好的做法?

您还需要帮助吗?@虽然确实需要,但现在我的问题也越来越大了。