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)