【发布时间】:2015-11-19 00:07:26
【问题描述】:
我检查了所有问题,但没有找到任何线索。我将我的问题简化为最简单的代码:
情况: 我想要:
CatComponent catComponent = DaggerCatComponent.builder()
.kameModule(new KameModule(MainActivity.this))
.build();
catComponent.getCatAnalyzer().analyze();
我已经创建了组件:
@Component(modules = {KameModule.class, CatAnalyzerModule.class})
public interface CatComponent {
CatAnalyzer getCatAnalyzer();
}
和模块:
@Module
public class KameModule {
private Context context;
public KameModule(Context context) {
this.context = context;
}
@Provides
KameCat provideKameCat() {
return new KameCat(context );
}
}
@Module(includes = KameModule.class)
public class CatAnalyzerModule {
@Inject
KameCat cat;
@Provides
CatAnalyzer provideCatAnalyzer() {
return new CatAnalyzer(cat);
}
}
和类:
public class KameCat {
Context context;
public KameCat(Context context) {
this.context = context;
}
public void doCatStuff() {
Toast.makeText(context, "Poo and Meow", Toast.LENGTH_LONG).show();
}
}
public class CatAnalyzer {
@Inject
KameCat cat;
@Inject
public CatAnalyzer(KameCat cat) {
this.cat = cat;
}
void analyze() {
cat.doCatStuff();
}
}
当我从 CatComponent 检索我的 CatAnalyzer 对象时,它有 cat 字段 nulled。
我不知道为什么 Dagger 不会注入它。你能指导我吗?
【问题讨论】:
标签: android module null inject dagger-2