0

I am trying to implement dagger2 for the dependency injection. Previously I was using Roboguice for the dependency injection but when I updated it to RG3 it stopped working. All the injected views were null. So I am trying to use Dagger2. Never had experience with dagger1.

Following are the classes that I am trying to use.

@Module
public class NTMAModule {

NTMA app;

public NTMAModule(final NTMA app) {
    this.app = app;
}

// EdgeConnection
@Provides
@Singleton
EdgeConnection provideEdgeConnection() {
    return new EdgeConnection();
}

// MessageCenterConnection
@Provides
@Singleton
MessageCenterConnection provideMessageCenterConnection() {
    return new MessageCenterConnection();
}

// ConnectionManager
@Provides
@Singleton
ConnectionManager provideConnectionManager() {
    return new ConnectionManager();
}

// AccountManager
@Provides
@Singleton
AccountManager provideAccountManager(EdgeConnection connection) {
    return new AccountManager(connection);
}
....
}

So in the module above I instantiate all the objects that I need.

Following is my componentclass

@Singleton
@Component(modules = {NTMAModule.class})
public interface NTMAComponent {
ConnectionManager connectionManager();
EdgeConnection edgeConnection();
MessageCenterConnection messageCenterConnection();
AccountManager accountManager();
void inject(Order order);
void inject(Position pos);
void inject(GroupObject gp);
void inject(LoginFragment fragment);
void inject(ReconnectingFragment fragment);
void inject(EdgeConnection connection);
void inject(LoginActivity act);
void inject(BaseActivity act);
void inject(ConnectionManager cm);
void inject(MessageCenterManager mcm);
}

This is my injector class

public enum Injector {

INSTANCE;

NTMAComponent applicationComponent;

Injector() {
}

void initializeInjector(NTMA application) {
    NTMAComponent component = DaggerNTMAComponent.builder().nTMAModule(new            NTMAModule(application)).build();
    applicationComponent = component;
}

public NTMAComponent getComponent() {
    return applicationComponent;
}
}

The injector is being initialized in the Application Class onCreate().

and then onCreate of different activities and for the classes like ConnectionManager, AccountManager, EdgeConnection i use it in the constructor's to inject field members using the component to inject them.

Injector.INSTANCE.getComponent().inject(this);

But at the launch of the application i get the following log...and it just keeps going. and then the stackoverflows and the app crashes. Nothing shows up at all.

01-04 15:30:36.761 12125-12125/? A/art: sart/runtime/runtime.cc:292]
at dagger.internal.ScopedProvider.get(ScopedProvider.java:46) 01-04 15:30:36.761 12125-12125/? A/art: sart/runtime/runtime.cc:292] - locked <0x13515e62> (a dagger.internal.ScopedProvider)

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
  • i'm a bit confuse with your injector class. why you didn't init the injection directly in application class or make your injector class as an singleton helper class instead of enum. – surya Jan 05 '16 at 01:57
  • I can make it a singleton class and use it to access it in other classes by Injector.INSTANCE.getComponent().inject(this), but in the application class i m initializing it using Injector.INSTANCE.initializeInjector(this) but anyways this does not matter. Do you know why I am getting the runtime.cc error error at the line – Monil Patel Jan 05 '16 at 04:44
  • It sounds like you have a cyclic dependency, but as you're using field injection, the compile time check doesn't warn you about it. Dagger2 cannot resolve a cyclic dependency through field injection, unfortunately. – EpicPandaForce Jan 12 '16 at 11:26
  • Yes it was a cyclic dependency, but figured out the solution. I injected all them members into my base activities using the component interface and also initializing the injection members while initializing the component. I thought dagger 2 automatically creates the instances for you when you need them unfortunately I had to call the methods from the components to initialize the instances and then the injection would happen fine. – Monil Patel Jan 13 '16 at 17:53

0 Answers0