我有一个ToolbarActivity
,它是所有其他活动的父级。除了toolbarActviity
之外,我所有的活动都有注入-改造,存储和查看。我也想注入到ToolbarPresenter(注入到ToolbarActivity
中)的改造,存储和查看中。但是我有一个错误:
error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] ToolbarActivity cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
A binding with matching key exists in component: InjectorBuilder_ProvideToolbarActivityInjector.ToolbarActivitySubcomponent
ToolbarActivity is injected at
provideToolbarPresenter(…, toolbarActivity)
ToolbarPresenter is injected at
ToolbarActivity.presenter
SortingPlansActivity is injected at
dagger.android.AndroidInjector.inject(T)
component path: AppComponent ? SortingPlansActivitySubcomponent
该如何解决?
ToolbarActivity.class
public class ToolbarActivity extends AppCompatActivity implements ToolbarView {
Menu menu;
SharedPreferences sharedPreferences;
public ImageView goHome;
public TextView currentOps;
@Inject
ToolbarPresenter presenter;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
AndroidInjection.inject(this);
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_main, menu);
ToolbarPresenter.class
public class ToolbarPresenter {
AuthInteractor authInteractor;
StorageHelper storageHelper;
ToolbarView view;
public ToolbarPresenter(AuthInteractor authInteractor, StorageHelper storageHelper, ToolbarView view) {
this.authInteractor = authInteractor;
this.storageHelper = storageHelper;
this.view = view;
}
}
ToolbarPresenterModule.class
@Module
public class ToolbarPresenterModule {
@Provides
public ToolbarPresenter provideToolbarPresenter(AuthInteractor authInteractor, StorageHelper storageHelper, ToolbarActivity toolbarActivity) {
return new ToolbarPresenter(authInteractor, storageHelper, toolbarActivity);
}
}
AppComponent.class
@Component (modules = {/*All injected modules})
@Singleton
public interface AppComponent {
void inject(App app);
@Component.Builder
interface Builder {
Builder contextModule(ContextModule contextModule);
Builder storageModule(StorageModule storageModule);
AppComponent build();
}
}
App.class
public class App extends Application implements HasActivityInjector, HasFragmentInjector {
@Inject
DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;
@Inject
DispatchingAndroidInjector<Fragment> activityDispatchingFragmentInjector;
public void onCreate() {
super.onCreate();
DaggerAppComponent.builder()
.contextModule(new ContextModule(this))
.storageModule(new StorageModule(this))
.build()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return activityDispatchingAndroidInjector;
}
@Override
public AndroidInjector<Fragment> fragmentInjector() {
return activityDispatchingFragmentInjector;
}
}