gpt4 book ai didi

java - Dagger 不会为 Android 生成 Dagger 类

转载 作者:行者123 更新时间:2023-12-02 03:09:49 26 4
gpt4 key购买 nike

我正在尝试使用 Dagger 建立一个项目。现在,当我构建时,没有为组件生成任何 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 模块如下所示:

@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();
}
};
}
}

我尝试重建项目以查看它是否会生成类。

最佳答案

看起来ActivityScreenSwitcherServer是一个限定符。如果是这种情况,您可以将 ActivityHierarchyServer 与限定符绑定(bind),但在组件上公开 ActivityHierarchyServer(通过 RedditAppGraph)而不使用限定符。从 @Provides 方法中删除限定符或将其添加到组件接口(interface)中。

关于java - Dagger 不会为 Android 生成 Dagger 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com