android - RoboGuice @Inject -


in android app project, using roboguice .

in project, have singleton class a:

@contextsingleton public class a{    …    public void method1(){…} } 

then, have class b needs instance of a, so, in roboguice way, declare instance of inside class b injection :

public class b {   @inject private a ;     public void action(){         a.method1(); // call method1() of class a's instance    } } 

sometimes, got nullpointerexception instance of declared in class b. want verify 1 concept of roboguice:

is in order inject instance of custom class (e.g. class a) in class b, class b has either injected in roboactivity or injected class (e.g. class c) has injected in roboactivity?

you instantiate b somewhere (new b()) , need call injector manually.

when roboguice creates instance b automatically inject dependency a, when create b yourself, roboguice wil not know , have call inject code yourself. can done calling:

roboinjector injector = roboguice.getinjector(context); injector.injectmemberswithoutviews(yourobjectb); 

Comments