ArrayAdapter problem: Cannot refer to an instance field while explicitly invoking a constructor

850 views
Skip to first unread message

glenviewjeff

unread,
Jun 26, 2011, 9:48:11 PM6/26/11
to robo...@googlegroups.com
I've got a custom ArrayAdapter that I want to inject, but if I inject the activity into the custom ArrayAdapter, I get the following error on the super(context, textViewResourceId, objects); call:

Cannot refer to an instance field activity while explicitly invoking a constructor.

This error is because Java thinks that the fields can't possibly be initialized prior to the call to super.  Of course, I'm injecting the context.  Does anyone know how to get around this problem?  I'd very much like to avoid making the injected members static. 

public class MyListAdapter extends ArrayAdapter<MyThing> {
    @Inject

    private MyListAdapterData myListAdapterData;
    @Inject
    private Activity activity;

    public MyListAdapter(int textViewResourceId) {
        super(activity, textViewResourceId, myListAdapterData.getSubCategories());
               ...


Michael Burton

unread,
Jun 27, 2011, 1:15:01 PM6/27/11
to robo...@googlegroups.com
Can you use constructor injection?  If so, you can use the following technique to inject the Actiity and myListAdapterData via your constructor while still providing the textViewResourceId manually:

--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/roboguice/-/oB1lco3_I5UJ.
To post to this group, send email to robo...@googlegroups.com.
To unsubscribe from this group, send email to roboguice+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/roboguice?hl=en.

glenviewjeff

unread,
Jun 27, 2011, 1:20:05 PM6/27/11
to robo...@googlegroups.com
Yes, that's what I ended up doing.  I thought I hadn't actually posted this or I would have posted the solution.  Thanks!
Reply all
Reply to author
Forward
0 new messages