Android - Dagger 注入未编译
Android - Dagger Injection not compiling
我正在向我的 Android 项目添加 Dagger 依赖项注入。尽管 Android Studio 在尝试编译之前没有显示任何错误,但出于某种原因我无法成功构建代码。
我在我的应用程序中使用了 MVPC 结构,并按如下方式实现了它:
public class Application extends com.activeandroid.app.Application {
private static Application instance;
private ApplicationComponent component;
public ApplicationComponent component() {
return component;
}
@Singleton @Component(modules = { AboutActivityModule.class })
public interface ApplicationComponent {
void inject(AboutActivity aboutActivity);
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
component = DaggerApplication_ApplicationComponent.create();
}
}
我的模块看起来是这样的:
@Module public class AboutActivityModule {
@Provides @Singleton AboutActivityPresenter provideAboutActivityPresenter(AboutActivityController aboutActivityController) {
return new AboutActivityPresenter(aboutActivityController);
}
}
这是我的主持人:
public class AboutActivityPresenter {
private AboutActivityController aboutActivityController;
public AboutActivityPresenter(AboutActivityController aboutActivityController) {
this.aboutActivityController = aboutActivityController;
}
public AboutActivityPresenter register(AboutActivityController aboutActivityController) {
this.aboutActivityController = aboutActivityController;
if (aboutActivityController != null) {
aboutActivityController.displayAboutPageFromLocalResources();
aboutActivityController.displayAppVersionNumber();
}
return this;
}
}
我的控制器:
public interface AboutActivityController{
void displayAboutPageFromLocalResources();
void displayAppVersionNumber();
}
最后但同样重要的是,我的 activity:
public class AboutActivity extends CringleActivity implements AboutActivityController {
@Inject AboutActivityPresenter presenter;
private ActivityAboutBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Application) getApplication()).component().inject(this);
setContentView(R.layout.activity_about);
binding = DataBindingUtil.setContentView(this, R.layout.activity_about);
presenter.register(this);
}
@Override
protected void onResume() {
super.onResume();
}
}
当我尝试构建它时,它失败了并出现以下错误:
/yo/yo/yo/yo-yo/yo/yo/yo/yo/yo/yo/yo/yo//FeedbackDialog.java
Error:(18, 39) error: yo.yoyo.yoyoyo.databinding does not exist
/yo/yo/yo/yo-yo/yo/yo/yo/yo/yo/yo/yo/yo/YoFragment.java
Error:(17, 39) error: package yo.yoyo.yoyoyo.databinding does not exist
Error:(31, 5) error: cannot find symbol class FragmentYoBinding
/yo/yo/yo/yo-yo/yo/yo/yo/yo/yo/yo/yo/yo/YoYoActivity.java
Error:(11, 39) error: package yo.yoyo.yoyoyo.databinding does not exist
Error:(57, 13) error: cannot find symbol class ActivityPinlockBinding
据我了解,databinding 从编译包生成代码,因此如果我编写的项目无法构建,databinding 将无法从中生成代码。我相当确定我的问题不在于数据绑定,而是我在使用 Dagger 时做错了什么。当我在我的 activity 中注释掉演示者的注入时(当然它在 onCreate 中注册了 activity),整个事情构建得很好。 Dagger 生成组件和提供者工厂。但是,出于某种原因,我不能注入演示者并拥有一个正常运行的应用程序。我尝试通过模块将 Integer 对象注入 activity,并且成功了(!),所以我不明白为什么我无法通过我的模块注入我的演示者。
@Module 中没有提供关于 AboutActivityController 的信息,但是 AboutActivityPresenter 需要它来进行初始化。
尝试像下面这样编辑您的模块class:
@Module
public class AboutActivityModule {
@Provides
@Singleton
public AboutActivityController provideAboutActivityController() {
// create and return the AboutActivityController instance
}
@Provides
@Singleton
public AboutActivityPresenter provideAboutActivityPresenter(AboutActivityController aboutActivityController) {
return new AboutActivityPresenter(aboutActivityController);
}
}
我正在向我的 Android 项目添加 Dagger 依赖项注入。尽管 Android Studio 在尝试编译之前没有显示任何错误,但出于某种原因我无法成功构建代码。
我在我的应用程序中使用了 MVPC 结构,并按如下方式实现了它:
public class Application extends com.activeandroid.app.Application {
private static Application instance;
private ApplicationComponent component;
public ApplicationComponent component() {
return component;
}
@Singleton @Component(modules = { AboutActivityModule.class })
public interface ApplicationComponent {
void inject(AboutActivity aboutActivity);
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
component = DaggerApplication_ApplicationComponent.create();
}
}
我的模块看起来是这样的:
@Module public class AboutActivityModule {
@Provides @Singleton AboutActivityPresenter provideAboutActivityPresenter(AboutActivityController aboutActivityController) {
return new AboutActivityPresenter(aboutActivityController);
}
}
这是我的主持人:
public class AboutActivityPresenter {
private AboutActivityController aboutActivityController;
public AboutActivityPresenter(AboutActivityController aboutActivityController) {
this.aboutActivityController = aboutActivityController;
}
public AboutActivityPresenter register(AboutActivityController aboutActivityController) {
this.aboutActivityController = aboutActivityController;
if (aboutActivityController != null) {
aboutActivityController.displayAboutPageFromLocalResources();
aboutActivityController.displayAppVersionNumber();
}
return this;
}
}
我的控制器:
public interface AboutActivityController{
void displayAboutPageFromLocalResources();
void displayAppVersionNumber();
}
最后但同样重要的是,我的 activity:
public class AboutActivity extends CringleActivity implements AboutActivityController {
@Inject AboutActivityPresenter presenter;
private ActivityAboutBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Application) getApplication()).component().inject(this);
setContentView(R.layout.activity_about);
binding = DataBindingUtil.setContentView(this, R.layout.activity_about);
presenter.register(this);
}
@Override
protected void onResume() {
super.onResume();
}
}
当我尝试构建它时,它失败了并出现以下错误:
/yo/yo/yo/yo-yo/yo/yo/yo/yo/yo/yo/yo/yo//FeedbackDialog.java
Error:(18, 39) error: yo.yoyo.yoyoyo.databinding does not exist
/yo/yo/yo/yo-yo/yo/yo/yo/yo/yo/yo/yo/yo/YoFragment.java
Error:(17, 39) error: package yo.yoyo.yoyoyo.databinding does not exist
Error:(31, 5) error: cannot find symbol class FragmentYoBinding
/yo/yo/yo/yo-yo/yo/yo/yo/yo/yo/yo/yo/yo/YoYoActivity.java
Error:(11, 39) error: package yo.yoyo.yoyoyo.databinding does not exist
Error:(57, 13) error: cannot find symbol class ActivityPinlockBinding
据我了解,databinding 从编译包生成代码,因此如果我编写的项目无法构建,databinding 将无法从中生成代码。我相当确定我的问题不在于数据绑定,而是我在使用 Dagger 时做错了什么。当我在我的 activity 中注释掉演示者的注入时(当然它在 onCreate 中注册了 activity),整个事情构建得很好。 Dagger 生成组件和提供者工厂。但是,出于某种原因,我不能注入演示者并拥有一个正常运行的应用程序。我尝试通过模块将 Integer 对象注入 activity,并且成功了(!),所以我不明白为什么我无法通过我的模块注入我的演示者。
@Module 中没有提供关于 AboutActivityController 的信息,但是 AboutActivityPresenter 需要它来进行初始化。
尝试像下面这样编辑您的模块class:
@Module
public class AboutActivityModule {
@Provides
@Singleton
public AboutActivityController provideAboutActivityController() {
// create and return the AboutActivityController instance
}
@Provides
@Singleton
public AboutActivityPresenter provideAboutActivityPresenter(AboutActivityController aboutActivityController) {
return new AboutActivityPresenter(aboutActivityController);
}
}