我正在使用匕首2和改造
我有两个模块

1.应用模块

2.博客模块

@Module
public class BlogModule {

   @PerActivity
    @Provides
    ApiService provideApiService(Retrofit retrofit){

        return retrofit.create(ApiService.class);
    }
}


应用组件

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

    Retrofit exposeRetrofit();
    Context exposeContext();
}


而且我在BlogComponent中添加了依赖项

@PerActivity
@Component(modules = BlogModule.class,dependencies = ApplicationComponent.class)
public interface BlogComponent {
    void inject(BaseActivity mainActivity);
}


在我的申请中

public class BlogApplication extends Application {
    private ApplicationComponent mApplicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        initializeApplicationComponent();
    }


    private void initializeApplicationComponent() {

       mApplicationComponent= DaggerApplicationComponent
               .builder().applicationModule(new ApplicationModule(this))

                .build();
    }

    public ApplicationComponent getApplicationComponent(){

        return  mApplicationComponent;
    }


当我尝试在基本活动中注入BlogComponent时,无法在getApplicationComponent()中添加applicationComponent(getApplicationComponent())

DaggerBlogComponent.builder()
          .applicationComponent()
          .blogModule(new BlogModule())
          .build().inject(this);


根据教程,他们注入如下

DaggerCakeComponent.builder()
        .applicationComponent(getApplicationComponent())
        .cakeModule(new CakeModule(this))
        .build().inject(this);


谁能帮我解决这个问题。谢谢

最佳答案

您应该可以从您的应用程序访问它

DaggerBlogComponent.builder()
          .applicationComponent(((BlogApplication)getApplication()).getApplicationComponent())
          .blogModule(new BlogModule())
          .build().inject(this);

07-24 09:49
查看更多