I have a class
public class DialogUtils
{
private Context context;
@Inject
public DialogUtils(Context context)
{
this.context = context;
}
}
In my activity class i have did but i'm getting null pointer exception on dialogUtils instance.
public class LoginActivity extends Activity{
@Inject DialogUtils dialogUtils;
}
I know how to inject dependency via module and component but not sure how to with construction injection. Any help is much appreciated.
On the one hand you are registering
DialogUtils
for Constructor Injection, so any component could provide it.On the other hand an activity and other parts of the Android Framework still need to be field injected. Dagger can't call their constructor, and
@Inject DialogUtils dialogUtils;
will not just magically appear.Here you have to use a component, and register a method that takes your components type as an argument. Dagger will then create the method to inject your activities fields.
To inject the fields you still have to create your component, and call the
inject(loginActivity)
method.If you don't retain the activity-level component and you aren't inheriting from a superscope (application-level component) using component dependency or subcomponent, then it's the following
then
And then