Java 构造函数注入找不到符号方法注入成员
我正在开发一个Android应用程序,它具有匕首依赖性。当通过构造函数注入注入类时,它抛出一个找不到符号的错误。如果我通过模块内部定义的Java 构造函数注入找不到符号方法注入成员,java,android,dependency-injection,dagger-2,dagger,Java,Android,Dependency Injection,Dagger 2,Dagger,我正在开发一个Android应用程序,它具有匕首依赖性。当通过构造函数注入注入类时,它抛出一个找不到符号的错误。如果我通过模块内部定义的@Provides方法提供依赖关系,那么一切都可以正常工作 守则: public class SixthGenericTest { @Inject FirstTest firstTest; @Inject public SixthGenericTest() {
@Provides
方法提供依赖关系,那么一切都可以正常工作
守则:
public class SixthGenericTest {
@Inject
FirstTest firstTest;
@Inject
public SixthGenericTest()
{
Injection.create().getAppComponent().inject(this);
}
public String getData(){
return firstTest.getTestName();
}
}
@Singleton
@Component(modules = {FirstModule.class})
public interface AppComponent {
void inject(SixthGenericTest sixthGenericTest);
}
我得到的错误是:
错误:(19,28)错误:找不到符号方法
InjectionMembers(MembersInjector,第六次通用测试)
您不需要注入
Injection.create().getAppComponent().injecte(这个)
@Inject
带注释的constructeur已经足够了,因为它会自动添加到图形中,您可以在类名上方添加您的作用域
@Singleton
public class SixthGenericTest {
FirstTest firstTest;
@Inject
public SixthGenericTest( FirstTest firstTest )
{
this.firstTest = firstTest;
}
public String getData()
{
return firstTest.getTestName();
}
}
并从组件中删除:
void inject(SixthGenericTest sixthGenericTest);
升级你的匕首版本
dependencies {
compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
}
我想在其他类中注入这个第六个GenericTest,我想将这个对象提供给其他类,这也是关键所在!您可以在其他类中提供第六个GenericTest@constructureur的Inject意思是“将我添加到图中”,就像您在模块类中使用提供方法时所做的那样。提供了一个我已经完成的方法,但我希望使用构造函数注入,这是可能的还是不可能的?这是2.15现在所需要的吗?在2.15之前(我测试了2.13),我从来没有得到@skyshine报告的错误。当我从2.13升级到2.15时,这种情况就会发生。因此,或许可以尝试使用2.13版而不是2.15版。我不知道为什么2.15会破坏我与2.14一起工作的代码。有趣的是,错误出现在生成的code.Key中——确保所有“dagger”版本都相同。确保compile和annotationProcessor使用相同的版本。我的问题是一个是2.11,另一个是2.15。