2

I'm using RoboGuice for the first time in a project and am trying to inject a static variable but the variable is remaining null. Here is a quick mockup of something I'm testing:

public class MyActivity extends RoboFragmentActivity {

    @Override protected void onCreate(Bundle savedInstanceState) {
      MyObject.print();
    }
}


@ContextSingleton
public class MyObject {

    @Inject static AssetManager sAssetManager;

    public static void print() {
      if(sAssetManager == null) {
        Log.d("debug", "AssestManager is null");
      } else {
        Log.d("debug", "AssetManager was injected");
      }
  }
}

How can I make this work ?

READ

I know this is not best practice - this is more a learning exercise in terms of using RoboGuice. Only answer if you have answer to the question given, not "Why are you doing this".

jjNford
  • 5,170
  • 7
  • 40
  • 64

1 Answers1

1

Why are you using a static method/variables? That's not the right way to do this. You should @Inject MyObject into MyActivity. Then you can do myObjectInstance.print();. Note that RoboGuice performs injection after setContentView() unless you force it.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Had a feeling this type of answer were coming. I wouldn't normally do this. The reason I need this is hard to explain, this is just a dumbed down example as well as a learning RoboGuice experience. Obviously I know I wouldn't normally do this. Just looking for the answer to this question - not a best practice. – jjNford Dec 12 '12 at 20:13
  • I think in SO we generally assume the worst. Anyway, I believe you still need to inject `MyObject` first so that the fields are injected. Did you try that? – dmon Dec 12 '12 at 20:26
  • Yes - so I just want the AssetManager object injected in this case. But for some reason its still null... wasn't sure if it was because of the 'static' keyword, or the face that MyObject can't find a context - however I thought the Provider for the AssetManager just used an Application context. – jjNford Dec 12 '12 at 20:38
  • Seems like the [AssetManagerProvider](http://code.google.com/p/roboguice/source/browse/roboguice/src/main/java/roboguice/inject/AssetManagerProvider.java) is using Context, which means it's not the Application. You can compare it with the [ResourcesProvider](http://code.google.com/p/roboguice/source/browse/roboguice/src/main/java/roboguice/inject/ResourcesProvider.java), which does use the Application context. – dmon Dec 12 '12 at 20:53
  • wow I misread that then, for some reason I was thinking it was the application. However I still have the same problem if instead of '@Inject AssetManager' I do an '@Inject Application'. Any reason for this? – jjNford Dec 12 '12 at 20:54
  • Seems like you have to call `requestStaticInjection()` from your module. http://www.rosscode.com/blog/index.php?title=android-development-part-3-wiring-up-roboguice&more=1&c=1&tb=1&pb=1 – dmon Dec 12 '12 at 21:27
  • Nice I saw that somewhere but this article looks to have a little more depth too it, I'll check it out. – jjNford Dec 12 '12 at 21:49
  • I think this will only work with RoboGuice-1, in version 2 you don't extends a GuiceApplication – jjNford Dec 12 '12 at 21:53
  • I extends AbstractModule and'''@Override protected void configure() { requestStaticInjection(DataCache.class); }''' then use https://code.google.com/p/roboguice/wiki/UpgradingTo20 to handle it. – Kevin May 06 '14 at 06:30