使用匕首2查看依赖注入

Sib*_*ini 7 java android dependency-injection view dagger-2

我有一个自定义视图扩展a TextView.我应该在哪里调用我的组件来注入视图?

component.inject(customTextView);
Run Code Online (Sandbox Code Playgroud)

Sib*_*ini 6

所以,我发现我需要在我的自定义视图的构造函数中添加注入(在所有这些中,或者调用另一个)

例:

public class CustomTextView extends TextView {
   @Inject
   AnyProvider anyProvider;

   public CustomTextView(Context context) { this(context, null); }
   public CustomTextView(Context AttributeSet attrs) { 
      super(context, attrs);
      Application.getComponent(context).inject(this);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 你在使用匕首2.11吗?你能告诉我谁得到这个:Application.getComponent(context)?除了AndroidInjector之外,无法使我的应用程序返回任何内容.谢谢. (2认同)