Search code examples
androidroboguice

Android RoboGuice with custom view not working


I have a custom view, and I want to use RoboGuice to get views references.

I used this example:https://github.com/roboguice/roboguice/wiki/Your-First-Injection-into-a-Custom-View-class

I tried both options (v3, v3.1), but it does not seem to work (in onFinishInflate, my members are null)

In case it matters, my custom view inherits from a base custom view with a generic type.

Does anyone know why can this happen?


Solution

  • Try adding a call to injectViewMembers, after the injectMembers, which takes care of non-view injections. Not sure why it is not documented and in the sample code.

    public ContactView(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.attrs = attrs;
            inflate(context,R.layout.contact_view, this);
            if (!isInEditMode()) {
                RoboGuice.getInjector(getContext()).injectMembers(this);
                RoboGuice.getInjector(getContext()).injectViewMembers(this);
    
            }
    
        }