我正在嘗試用Dagger設置一個項目。現在,當我建立,爲組件生成任何匕首的*類,我得到了以下錯誤:Dagger不會爲Android生成Dagger類
Error:(10, 8) error: [com.redditapp.dagger.RedditAppGraph.inject(com.redditapp.RedditApplication)]
com.redditapp.ui.ActivityHierarchyServer cannot be provided without an @Provides- or @Produces-annotated method.
com.redditapp.ui.ActivityHierarchyServer is injected at
com.redditapp.RedditApplication.activityHierarchyServer
com.redditapp.RedditApplication is injected at
com.redditapp.dagger.RedditAppGraph.inject(app)
和
Error:(13, 10) error: com.redditapp.base.navigation.activity.ActivityScreenSwitcher cannot be provided without an @Inject constructor or from an @Provides-annotated method.
com.redditapp.base.navigation.activity.ActivityScreenSwitcher is injected at
com.redditapp.ui.screens.home.HomePresenter.<init>(screenSwitcher)
com.redditapp.ui.screens.home.HomePresenter is injected at
com.redditapp.ui.screens.home.HomeActivity.presenter
com.redditapp.ui.screens.home.HomeActivity is injected at
com.redditapp.ui.screens.home.HomeComponent.inject(activity)
這裏是我的當前設置。
應用程序級類使用:
public void buildComponentAndInject() {
component = RedditAppComponent.Initializer.init(this);
component.inject(this);
}
的RedditAppComponent看起來像這樣,也不產生DaggerRedditAppComponent,因此是紅色:
@ApplicationScope
@Component(modules = { RedditAppModule.class, UiModule.class})
public interface RedditAppComponent extends RedditAppGraph {
/**
* An initializer that creates the graph from an application.
*/
final class Initializer {
public static RedditAppComponent init(RedditApplication app) {
return DaggerRedditAppComponent.builder()
.redditAppModule(new RedditAppModule(app))
.uiModule(new UiModule())
.build();
}
private Initializer() {} // No instances.
}
}
與父類RedditAppGraph:
public interface RedditAppGraph {
void inject(RedditApplication app);
ViewContainer viewContainer();
ActivityHierarchyServer activityHierarchyServer();
}
包含提供者方法的UI模塊是throwin g的錯誤看起來像︰
@Module
public class UiModule {
@Provides
@ApplicationScope
ActivityScreenSwitcher provideActivityScreenSwitcher() {
return new ActivityScreenSwitcher();
}
@Provides
@ApplicationScope
@ActivityScreenSwitcherServer
ActivityHierarchyServer provideActivityHierarchyServer(final ActivityScreenSwitcher screenSwitcher) {
return new ActivityHierarchyServer.Empty() {
@Override
public void onActivityStarted(Activity activity) {
screenSwitcher.attach(activity);
}
@Override
public void onActivityStopped(Activity activity) {
screenSwitcher.detach();
}
};
}
}
我試過重建項目,看看它是否會生成類。
你在應用編譯器嗎?你是否在你的build gradle中使用了'apt「com.google.dagger:dagger-compiler:2.8」''? – Fred
我這樣做,但不是apt我正在使用annotationProcessor。 – AggieDev