Dagger Android View Injection Error

346 Views Asked by At

I'm having a heck of a time figuring out how to set up Dagger dependencies to satisfy what is essentially this snippet.

I have a controller, WelcomeScreen, (which extends Path - a Mortar/Flow thing), which states that it injects a ThingView. I then inject a provider of ThingView and my ThingView constructor has an @Inject annotation with an Activity, which I supply elsewhere.

I still end up getting this error: No inject registered for members/com.....view.ThingView. You must explicitly add it to the 'injects' option in one of your modules.

Thoughts on what I'm missing?

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);
  }
}

Update: I've also tried adding the below which has had no effect on the error message:

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

There are 1 best solutions below

5
FriendlyMikhail On

Thats not quite how Mortar works. You cannot inject the view into the presenter. Instead you inject your presenter to your view. Here's aa few samples I found that shows a similar setup to what you are doing https://github.com/Zhuinden/MortarFlowInitialDemo https://github.com/matthew-compton/NerdRoll