Different ways to inject Context

63 views
Skip to first unread message

John A

unread,
Sep 17, 2013, 11:58:17 AM9/17/13
to robo...@googlegroups.com
Hi,

sorry if it sounds like a duplicate, but I was wondering what's the differences between injecting a Context, and Application and a Provider<Context>?
So far, I've been Injecting Context, and everything was fine, but yesterday, I had a crash:

Error: 
    java.util.EmptyStackException
   at java.util.Stack.peek(Stack.java:57)
   at roboguice.inject.ContextScope$1.get(ContextScope.java:108)
   at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
   at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
   at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
   at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
   at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
   at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
   at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
   at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
   at com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:632)
   at $Proxy6.createChannelViewHolder(Native Method)

I googled and found that it could be Context related. So I Injected an Application (and called getApplicationContext), and the problem went away. Same with using Provider<Context>

I need a Context in order to:
- manipulate files (openFileInput, deleteFile, getFileStreamPath)
- start an IntentService
- get Inflater (I'm generating ViewHolder on the fly, to be populated in an Adapter)

Those tasks happen without UI, so I can't really pass an Activity or a Context all the was down to them.

so I'm not sure what I should use...

Thanks for your help!

Marco Serioli

unread,
Sep 17, 2013, 12:05:43 PM9/17/13
to robo...@googlegroups.com

We get this error when using the injected resource first from an activity and then from a service.

I think that this is why the first Activity or Service that create singleton in which is the context, set also the context to be the context of an activity or of service.

So if the injected resource is injected by an activity you won't have exception until the same class is called from a service.

Could it be?

Using ApplicationContext solve the issue for us..

--
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/groups/opt_out.

John A

unread,
Sep 17, 2013, 1:12:19 PM9/17/13
to robo...@googlegroups.com
update: turns out that this error occurs when I'm using assisted injection:

private Context context;

@Inject
public MyClass(@Assited MyObject object, Provider<Context> contextProvider) {
    context = contextProvider.get(); // cash
}

@Inject
public MyClass(MyObject object, Provider<Context> contextProvider) {
    context = contextProvider.get(); // ok
}

If I inject a MyClass object, that's ok, but if I build it trough a factory, I get a crash (unless I inject an Application and use getApplicationContext()).

Is there anything missing on my side? Thanks
Reply all
Reply to author
Forward
0 new messages