首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RoboGuice.getInjector抛出异常

RoboGuice.getInjector抛出异常
EN

Stack Overflow用户
提问于 2014-07-01 17:42:52
回答 2查看 855关注 0票数 1

我尝试在我的安卓项目中使用RoboGuice 2,它工作正常,除非我尝试使用它在活动中注入视图。如果我试图从RoboActivity扩展我的活动,或者在onCreate方法中手动调用RoboGuice.getInjector(context),我会得到以下异常:

代码语言:javascript
复制
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添加到我的项目中

代码语言:javascript
复制
<dependency>
  <groupId>org.roboguice</groupId>
  <artifactId>roboguice</artifactId>
  <version>2.0</version>
</dependency>

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2014-07-02 08:39:32

默认情况下,RoboGuice将尝试使用默认构造函数创建对象。你的对象似乎没有默认的构造函数。您可以通过以下方式解决此问题:

  • 注释要与@Inject一起使用的构造函数(并确保该构造函数的所有参数也可由RoboGuice创建)
  • 在尝试注入的对象中创建默认构造函数
  • 如果您无法控制尝试注入的对象,则可以在注入阶段使用提供程序构造对象
票数 0
EN

Stack Overflow用户

发布于 2014-07-02 20:36:26

我发现,如果我只是简单地扩展AbstractModule,正确配置它需要比简单地绑定上下文多得多的工作。

我按照javadoc中的解释扩展了DefaultRoboModule:

代码语言:javascript
复制
Injector injector =
    RoboGuice.setBaseApplicationInjector(
        this, Stage.PRODUCTION,
        Modules.override(RoboGuice.newDefaultRoboModule(this)).with(new MyModule())
    );

并且让一切都像预期的那样工作。

不管怎样,谢谢你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24507108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档