有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Dagger Android视图注入错误

我花了很长时间才弄明白如何设置匕首依赖项来满足这段代码的本质

我有一个控制器WelcomeScreen(它扩展了路径——一个砂浆/流动的东西),它声明它注入了一个ThingView。然后我注入ThingView的一个提供者,我的ThingView构造函数有一个带有活动的@inject注释,我在别处提供了这个注释

我最终还是遇到了这个错误:没有为members/com注册任何注入。。。。。看法ThingView。必须将其显式添加到其中一个模块的“injects”选项中

想想我错过了什么

public class WelcomeScreen extends Path {
  @dagger.Module(
      injects = {
          ThingView.class,
      },
      addsTo = ActivityModule.class)
  public class Module {
  }

  @Singleton
  public static class Presenter extends ViewPresenter<WelcomeView> {
    @Inject
    public Presenter(
        Provider<ThingView> thingViewProvider,) {
      // This causes an error: No inject registered for members/com.....view.ThingView.
      // You must explicitly add it to the 'injects' option in one of your modules.
      thingViewProvider.get()
    }
  }
}

public class ThingView extends LinearLayout {
 @Inject
  public ThingView(Activity activity) {
    super(activity);
    init(activity);
  }

  // Note - this might not be used anywhere.
  public ThingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }

  private void init(Context context) {
    ObjectGraphService.inject(context, this);
    View view = View.inflate(context, R.layout.view_thread_pack, this);
    ButterKnife.inject(view);
  }
}

更新:我还尝试添加以下内容,但对错误消息没有影响:

@Module(addsTo = ApplicationModule.class, library = true)
public class ActivityModule {  
  @Provides
  public ThreadPackView providesThreadPackView() {
    return new ThreadPackView(activity);
  }
}

共 (1) 个答案