Injecting EventManager into a ImageButton and getting null pointer exception

43 views
Skip to first unread message

Alexander Koch

unread,
Aug 8, 2014, 1:27:49 PM8/8/14
to robo...@googlegroups.com
Hello,

I am new to RoboGuice so maybe I am misunderstanding something. I am using the EventManager to notify my Action and this works well from a RoboListFragment with @Inject. 

But I am having trouble using it in a class that extends ImageButton. I want to have an ImageButton that fires an event when clicked which contains the buttons id:

public class AddItemButton extends ImageButton implements OnClickListener {
  @Inject
 
private EventManager eventManager;
 
 
public AddItemButton(Context context) {
   
super(context);
    setOnClickListener
(this);
 
}
 
 
public AddItemButton(Context ctx, AttributeSet attrs) {
   
super(ctx, attrs);
    setOnClickListener
(this);
 
}

 
@Override
 
public void onClick(View view) {
   eventManager
.fire(new ButtonClickEvent(getId()));
 
}
}


But this gives me a null pointer exception because eventManager is null. 

08-08 19:11:41.262: E/AndroidRuntime(20272): java.lang.NullPointerException

08-08 19:11:41.262: E/AndroidRuntime(20272): at org.ak80.alar.view.AddItemButton.onClick(AddItemButton.java:36)

08-08 19:11:41.262: E/AndroidRuntime(20272): at android.view.View.performClick(View.java:4438)

08-08 19:11:41.262: E/AndroidRuntime(20272): at android.view.View$PerformClick.run(View.java:18422)

08-08 19:11:41.262: E/AndroidRuntime(20272): at android.os.Handler.handleCallback(Handler.java:733)

08-08 19:11:41.262: E/AndroidRuntime(20272): at android.os.Handler.dispatchMessage(Handler.java:95)

08-08 19:11:41.262: E/AndroidRuntime(20272): at android.os.Looper.loop(Looper.java:136)

08-08 19:11:41.262: E/AndroidRuntime(20272): at android.app.ActivityThread.main(ActivityThread.java:5001)

08-08 19:11:41.262: E/AndroidRuntime(20272): at java.lang.reflect.Method.invokeNative(Native Method)

08-08 19:11:41.262: E/AndroidRuntime(20272): at java.lang.reflect.Method.invoke(Method.java:515)

08-08 19:11:41.262: E/AndroidRuntime(20272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)

08-08 19:11:41.262: E/AndroidRuntime(20272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

08-08 19:11:41.262: E/AndroidRuntime(20272): at dalvik.system.NativeStart.main(Native Method)


I think it maybe that EventManager is missing the Context / Activity. Can someone please help me to understand how to achieve my goal?

Mike Burton

unread,
Aug 8, 2014, 3:54:08 PM8/8/14
to robo...@googlegroups.com
This may be helpful:



--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to roboguice+...@googlegroups.com.
To post to this group, send email to robo...@googlegroups.com.
Visit this group at http://groups.google.com/group/roboguice.
For more options, visit https://groups.google.com/d/optout.

Alexander Koch

unread,
Aug 8, 2014, 4:02:54 PM8/8/14
to robo...@googlegroups.com
Indeed it was. I feel stupid, I had no
  RoboGuice.getInjector(getContext()).injectMembers(this);
 
Thank you for your quick reply!

Mike Burton

unread,
Aug 8, 2014, 4:06:08 PM8/8/14
to robo...@googlegroups.com, robo...@googlegroups.com
No problem, it’s a limitation of the way we hook into views.  Normally you shouldn’t need to call roboguice manually yourself, but for views you currently do.


--
Reply all
Reply to author
Forward
0 new messages