使用Guice Assisted Inject Factory在AsyncTask中注入Context时出错

时间:2014-01-26 16:35:17

标签: java android guice roboguice assisted-inject

我在Guice Extension - Assisted Inject的帮助下使用RoboGuice 2.0创建Android项目。现在我面临着在非UI线程上通过工厂注入Context的问题。

示例项目显示我的问题:

人员界面:

public interface Person {
}

人员实施:

@AssistedInject
public PersonImpl(@Assisted String name, @Assisted int age, Context context) {
}

@Inject
public PersonImpl(Context context) {
}

Perfon工厂​​:

Person create(@Assisted String name, @Assisted int age);

模块:

private static class TestModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Person.class).to(PersonImpl.class);
        install(new FactoryModuleBuilder()
                .implement(Person.class, PersonImpl.class)
                .build(PersonFactory.class));
    }
}

MainActivity:

@Inject
private PersonFactory personFactory;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            Person p = RoboGuice.getInjector(MainActivity.this).getInstance(Person.class);
            Log.d("TAG", "By injector " + p.toString());
            p = personFactory.create("AAA", 123);
            Log.d("TAG", "By factory " + p.toString());
            return null;
        }
    }.execute();
}

执行AsyncTask时,注入器创建Person对象。此操作正常,并按预期返回PersonImpl对象。但是,当工厂尝试创建新的Person对象时,会引发此异常:

   java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: 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:41)
        at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
        at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
        at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:107)
        at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:88)
        at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:269)
        at com.google.inject.internal.InjectorImpl$3$1.call(InjectorImpl.java:1005)
        at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1051)
        at com.google.inject.internal.InjectorImpl$3.get(InjectorImpl.java:1001)
        at com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:665)
        at $Proxy6.create(Native Method)
        at eu.stosdev.MainActivity$1.doInBackground(MainActivity.java:25)
        at eu.stosdev.MainActivity$1.doInBackground(MainActivity.java:20)

如果在UI线程上执行了这些创建操作,则会正确创建PlayerImpl对象。我刚刚开始使用Google Guice而且我很困惑为什么当使用注入器创建对象时一切都很好,但是使用工厂不行。有没有什么好的解决方案如何在线程上使用工厂?

0 个答案:

没有答案