0

I am having problems in Injecting a module into my class using roboguice. I added roboguice_modules.xml to src/main/res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string-array name="roboguice_modules">
     <item>com.androidclient.guice.BindingModule</item>
   </string-array>
</resources>

My BindingModule looks like this

public class BindingModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(FooIf.class).to(FooImpl.class);
    }
}

and this is how i injected FooImpl into my class

@Inject  
FooIf foo;

Any idea on what I am doing wrong here?

JavaBeigner
  • 608
  • 9
  • 28
anonymous123
  • 1,271
  • 6
  • 19
  • 43

1 Answers1

0

Your MainActivity must extend RoboFragmentActivity no FragmentActivity . All classes then use injection must extend some class of Roboguice library, because there is when happen the magic, ie: RoboFragment, RoboActivity, RoboAsyncTask, RoboBroadcastReceiver, etc. The injection happen after setContentView method on onCreate method, after that call method, your variable is injected.

You can use this code:

public class MainActivity extends RoboFragmentActivity 
{
    @Inject  
    FooIf foo;

    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);

        //Here you can use foo variable
    }
 }
Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52
  • This is not correct. You do not have to extend the Robo* classes. You can explicitly trigger the injection yourself. – EJK Aug 27 '14 at 19:05
  • You can request RoboGuice.getInjector(context).getInstance(Class) to retieve a injected instance. But in this case, @anonymous123, want inject one field on FragmentActivity class, is unnecessary call RoboGuice.getInjector(this).injectMembers(this), just extend RoboFragmentActivity, otherwise, the attribute Inject does not exist. – Jose Rodriguez Aug 27 '14 at 19:11
  • I still think the wording of your answer should be changed. It is not true the you "must" extend these classes. This is just one option. And it is not a very good one as it introduces a hard dependency on these base classes. – EJK Aug 27 '14 at 20:28
  • Ok, @EJK, then injection base classes and Inject annotation on Roboguice are useless? In the end, all these classes extends the original classes, RoboFragmentActivity extends FragmentActivity, RoboActivity extends Activity, etc. Define "hard dependency", because, I can not inherit from Activity, FragmentActivity,etc, it is not a "hard dependency". – Jose Rodriguez Aug 27 '14 at 21:46
  • you are missing my original point. It was simply that it is incorrect for the answer to state that you "must" extend these classes. It is only one option... – EJK Aug 28 '14 at 01:42
  • As for why extending these classes is, in my opinion, not the best option: because it commits you to a specific class hierarchy. Note that RoboFragmentActivity extends android.support.v4.app.FragmentActivity. Thus you have a hard dependency on the support library. What if your app does not use the support library? Then this is not a good option for you. – EJK Aug 28 '14 at 01:47
  • One more point - I never said these classes were "useless". I'm sensing some anger in your comments and I'm not sure why. It's a shame that we cannot have a civil discussion on this matter. – EJK Aug 28 '14 at 01:50
  • Before continue commenting, you MUST change your answer because is wrong, you will receive a java.lang.IllegalArgumentException: xxx.xxx does not appear to be a RoboGuice context (instanceof RoboContext). And this is not correct. – Jose Rodriguez Aug 28 '14 at 15:50
  • Let me ask something, how do you use Roboguice? You always use Roboguice.getInjector(context).injectMembers(object), or Roboguice.getInjector(context).injectViewMembers(object) or Roboguice.getInjector(context).getInstance(Class)? You never use RoboFragmentActivity, RoboActivity, etc, because don´t want add a reference to support library? – Jose Rodriguez Aug 28 '14 at 15:57
  • Also, include Support Libraries is a good practice to maintain compatibility with other Android Frameworks API http://developer.android.com/tools/support-library/index.html – Jose Rodriguez Aug 28 '14 at 16:41
  • I am of the opinion that before criticizing others, I look at my first. – Jose Rodriguez Aug 28 '14 at 16:43