我尝试在我的安卓项目中使用RoboGuice 2,它工作正常,除非我尝试使用它在活动中注入视图。如果我试图从RoboActivity扩展我的活动,或者在onCreate方法中手动调用RoboGuice.getInjector(context),我会得到以下异常:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity}: com.google.inject.ConfigurationException: Guice configuration errors:
1) Could not find a suitable constructor in roboguice.inject.ContextScope. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at roboguice.inject.ContextScope.class(Unknown Source)
while locating roboguice.inject.ContextScope
1 error
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.google.inject.ConfigurationException: Guice configuration errors:
1) Could not find a suitable constructor in roboguice.inject.ContextScope. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at roboguice.inject.ContextScope.class(Unknown Source)
while locating roboguice.inject.ContextScope
1 error
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at roboguice.inject.ContextScopedRoboInjector.<init>(ContextScopedRoboInjector.java:27)
at roboguice.RoboGuice.getInjector(RoboGuice.java:149)
...我使用maven通过以下依赖项将guice添加到我的项目中
<dependency>
<groupId>org.roboguice</groupId>
<artifactId>roboguice</artifactId>
<version>2.0</version>
</dependency>任何帮助都将不胜感激。
发布于 2014-07-02 08:39:32
默认情况下,RoboGuice将尝试使用默认构造函数创建对象。你的对象似乎没有默认的构造函数。您可以通过以下方式解决此问题:
@Inject一起使用的构造函数(并确保该构造函数的所有参数也可由RoboGuice创建)发布于 2014-07-02 20:36:26
我发现,如果我只是简单地扩展AbstractModule,正确配置它需要比简单地绑定上下文多得多的工作。
我按照javadoc中的解释扩展了DefaultRoboModule:
Injector injector =
RoboGuice.setBaseApplicationInjector(
this, Stage.PRODUCTION,
Modules.override(RoboGuice.newDefaultRoboModule(this)).with(new MyModule())
);并且让一切都像预期的那样工作。
不管怎样,谢谢你。
https://stackoverflow.com/questions/24507108
复制相似问题