I have some interfaces and some concreted classes implement those interfaces. I have config in AbstractModule
class.
But my problem is: @Inject
just works inside a RoboGuice class such as RoboFragment
, RoboActivity
...
For example:
public class Fragment extends RoboFragment {
@Inject
ICustomClass helper; // work. helper will be initialized and call successfully.
}
public class JavaNormalClass {
@Inject
ICustomClass helper; // doesn't work. NullPointerException
}
So, I think RoboGuice
doesn't inject custom class when it's inside normal class so that I should call it by hand. (I guess !!!) So, how to fix my problem ?
Thanks :)
This simple solution work for me.
You put this line of code before you call any @Inject
member. Best way maybe in constructor of normal class or onCreate
of activity ...
public class CustomClass {
@Inject
IDBDAL dbDAL;
// can be application context or activity context
private Context mContext;
public CustomClass() {
// you can get context variable somewhere else
mContext = MyApplication.getAppContext();
RoboGuice.getInjector(mContext).injectMembersWithoutViews(this);
// from now. you can use dbDAL object
dbDAL.doSomething(); // work like charm :)
}
}
I think this solution is generic, and can apply to multiple situations. Hope this help :)