Cannot be provided without an @Provides- or @Produces-annotated method

时间:2016-02-12 21:37:30

标签: android dependency-injection dagger-2

I am faced with a very strange behaviour of Dagger 2. Maybe I just don't understand it.

So I have 2 components:

if(gpu_block == 32) dmv_gpu_shmem_templ<32><<<gpu_grid,gpu_block,shmem_size>>> (gpu_A, gpu_x, gpu_y, n);

AppComponent:

@Singleton @Component(modules = {/*my modules*/}) public interface AppComponent extends AppGraph { @ForApplication Context getContext(); }

ActivityComponent:

@ForActivity @Component(dependencies = AppComponent.class, modules = {ActivityModule.class}) public interface ActivityComponent extends AppComponent { void inject(BaseActivity activity); @ForActivity BaseActivity getActivity(); }

ForApplication:

@Qualifier @Retention(RUNTIME) public @interface ForApplication { }

ForActivity:

And also, I have an @Scope @Retention(RUNTIME) public @interface ForActivity { } , which has ActivityModule methods:

provides

So, when I inject @Module public class ActivityModule { private final BaseActivity activity; public ActivityModule(BaseActivity activity) { this.activity = activity; } @ForActivity @Provides public BaseActivity provideActivityContext(){ return activity; } @Provides public ContextWrapper provideContextWrapper(){ return new ContextWrapperImpl(activity); } @Provides public CustomNavigator provideMakeDepositNavigator(){ return new CustomNavigatorImpl(activity); } } into my activity, I get no errors.

But when I inject ContextWrapper, I get the error:

CustomNavigator cannot be provided without an @Provides- or @Produces-annotated method. Dagger 2

These 2 interfaces are the same. But CustomNavigatorImpl is injected, and ContextWrapper not

CustomNavigator:

ContextWrapper

public interface ContextWrapper { String getString(int resId); Context getContext(); }

MakeDepositNavigator:

In application class I create public interface MakeDepositNavigator { void showSomething1(); void showSomething2(); void showSomething3(); /*...*/ } :

DaggerAppComponent

In DaggerSdkAppComponent.builder() .applicationModule(new ApplicationModule(c)) .build(); I create BaseActivity:

ActivityComponent

Also, AppComponent component = ((HasComponent<AppComponent>) getApplicationContext()) .getComponent(); mComponent = DaggerActivityComponent.builder() .activityModule(new ActivityModule(this)) .appComponent(component) .build(); has a method:

BaseActivity

And in @Override public ActivityComponent getComponent() { return mComponent; } , which extends from CustomActivity I do:

BaseActivity

UPDATED

getComponent().inject(this);

Activity:

1 个答案:

答案 0 :(得分:1)

我有类似的测试问题。

@ForApplication Context getContext();
由于包可访问性(默认可访问性),子类AppComponent可能无法访问ActivityComponent中的

我认为添加public作为访问修饰符可以解决您的问题。