我在使用 Dagger 2 时遇到了类似的问题。我有一个 AppComponent 和一个 ActivityComponent(作为子组件)。一旦我在 AppComponent 中添加一个新的 inject() 函数,我就会得到上述错误。
除了“找不到符号”错误之外还有更多错误,但它们非常模糊,我无法调试我的问题。在挖掘和研究了 stackoverflow 和不同的教程之后,我意识到我错误地使用了 Dagger。特别是我的 AppComponent 和 ActivityComponent 的设置方式。
我假设我可以用我的 AppComponent 和 ActivityComponent 注入我的“Activity”或“Fragment”。事实证明这是错误的,至少我发现这不是使用 Dagger 的正确方法。
我的解决方案:
应用组件
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
void inject(MyApp application);
void inject(ContextHelper contextHelper);
// for exports
MyApp application();
PrefsHelper prefsHelper();
}
应用模块
@Module
public class AppModule {
private final MyApp application;
public AppModule(MyApp application) {
this.application = application;
}
@Provides @Singleton
public MyApp application() {
return this.application;
}
@Provides @Singleton
public PrefsHelper providePrefsHelper() {
PrefsHelper prefsHelper = new PrefsHelper(application);
return prefsHelper;
}
}
活动组件
@ActivityScope
@Component (dependencies = {AppComponent.class}, modules = {ActivityModule.class})
public interface ActivityComponent {
void inject(MainActivity activity);
void inject(OtherActivity activity);
void inject(SomeFragment fragment);
}
活动模块
@Module
public class ActivityModule {
private final MyActivity activity;
public ActivityModule(MyActivity activity) {
this.activity = activity;
}
@Provides @ActivityScope
public ContextHelper provideContextHelper(MyApp application) {
// My ContextHelper depends on certain things from AppModule
// So I call appComponent.inject(contextHelper)
AppComponent appComponent = application.getAppComponent();
ContextHelper contextHelper = new ContextHelper(activity);
appComponent.inject(contextHelper);
return contextHelper;
}
}
应用
public class MyApp extends Application {
private AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
initializeDepInj();
}
private void initializeDepInj() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
}
public LockAppComponent getAppComponent() {
return appComponent;
}
}
活动
public class MainActivity extends AppCompatActivity {
// I get it from ActivityModule
@Inject
ContextHelper contextHelper;
// I get it from AppModule
@Inject
PrefsHelper prefsHelper;
ActivityComponent component;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupInjection();
}
protected void setupInjection() {
MyApp application = (MyApp) getApplication();
component = DaggerActivityComponent.builder()
.appComponent(application.getAppComponent())
.activityModule(new ActivityModule(this))
.build();
component.inject(this);
// I was also doing the following snippet
// but it's not the correct way since the ActivityComponent depends
// on AppComponent and therefore ActivityComponent is the only
// component that I should inject() with and I'll still get classes
// that are provided by the AppComponent/AppModule
// application.getAppComponent().inject(this); // this is unneccessary
}
public ContextHelper getContextHelper() {
return contextHelper;
}
}
我不知道它是否直接解决了您的问题,但它至少应该对如何正确使用 Dagger 有所启发。
希望对你有帮助。