The problem with your code is the incorrect implementation of the HasSupportFragmentInjector or HasFragmentInjector (it depends on whether you use the support library for fragments or not).
You must implement this interface either in your activity in which the fragment is hosted, or in your Application class. Personally, I would recommend the following class of the application so that you do not bother to implement it in all actions that host the fragment:
public class MyApplication extends Application implements HasActivityInjector, HasSupportFragmentInjector { @Inject DispatchingAndroidInjector<Activity> mActivityInjector; @Inject DispatchingAndroidInjector<Fragment> mFragmentInjector; @Override public void onCreate() { super.onCreate();
And then you enter your snippets (as you already do in your code):
@Override public void onAttach(Context context) { AndroidSupportInjection.inject(this); super.onAttach(context); }
If the above does not work, be sure to create the appropriate components / subcomponents for the screen you are trying to enter. If you use @ContributesAndroidInjector instead of manually defining components, make sure you have one entry for your screen in your binding module:
@ContributesAndroidInjector(modules = MyScreenModule.class) abstract MyScreenActivity bindMyScreen();
If you still can not get it to work. I recommend reading the actual Dagger Documentation :
Hope this helps.
source share