RoboGuice has no chance to inject anything into your MyHandler
instance because you are using new
to create it, instead of having it created by your DI framework. I'm not familiar with RoboGuice (only plain Guice), but probably there there is a way to ask the injector to inject members after you created the instance by yourself. Though I think it's better to restucture the code so the instance is created by RoboGuice in the first place.
When class injected by RoboGuice @Inject could be null?
Pregunta
When roboguice's @Inject
could be null?
I have class:
@Singleton
public class MyClass extends AnotherClass{ ...
In many places I'm using
@Inject private MyClass mMyClass;
and it is working correctly.
Recently I wrote simple class handling events from event bus. This class is created and registered in main ui thread. In this class I'm injecting MyClass
same way like other places, but it is null
now.
@Override
public void onCreate() {
super.onCreate();
...
mMyHandler = new MyHandler(this);
mMyEventBus.register(mMyHandler);
public class MyHandler{
private final Context mContext;
public MyHandler(Context context){
mContext = context;
}
@Inject private MyClass mMyClass;
public void onEventBackgroundThread(MyEvent event) {
mMyClass.someMethod; //mMyClass is null
...
}
}
I'm using RoboGuice 2.0.
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow