Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Roboguice 2.0@将单例注入POJO_Android_Dependency Injection_Singleton_Roboguice - Fatal编程技术网

Android Roboguice 2.0@将单例注入POJO

Android Roboguice 2.0@将单例注入POJO,android,dependency-injection,singleton,roboguice,Android,Dependency Injection,Singleton,Roboguice,我创建了一个用@Singleton注释的设置类。这个类成功地注入到我的机器人活动中。然而,当我尝试将它注入到POJO(普通旧java对象)时,我会得到一个空指针异常(即未注入)。这个POJO是在另一个线程中实例化的(我不知道它是否相关)。最后一件事,如果我想注入一个类的实例,我是否必须显式地创建该类的默认构造函数? 谢谢你的帮助, 托马斯我的错, 问题是我没有使用: RoboGuice.getInjector(context).injectMembers(this); 此链接很好地解释了rob

我创建了一个用@Singleton注释的设置类。这个类成功地注入到我的机器人活动中。然而,当我尝试将它注入到POJO(普通旧java对象)时,我会得到一个空指针异常(即未注入)。这个POJO是在另一个线程中实例化的(我不知道它是否相关)。最后一件事,如果我想注入一个类的实例,我是否必须显式地创建该类的默认构造函数?
谢谢你的帮助,
托马斯

我的错, 问题是我没有使用:

RoboGuice.getInjector(context).injectMembers(this);

此链接很好地解释了roboguice的单例功能:

它解释了一个非常重要的警告/反模式

@Singleton
公共级天文男孩{
//因为Astroboy是单身汉,我们不能直接注入当前上下文
//因为当前环境可能会根据使用Astroboy的活动而变化
//相反,注入当前上下文的提供者,然后我们可以
//当我们需要上下文时,向提供者询问上下文。
//可控震源已绑定到RoboModule中的context.getSystemService(可控震源服务)。
//Random没有特殊绑定,因此Guice将为我们创建一个新实例。
@注入
提供者上下文提供者;
@注入
振动器;
@注入
随机;
公开说(串某物){
//使用上下文返回的当前上下文制作祝酒词
//提供者
Toast.makeText(contextProvider.get(),
“阿童木说,\”“+something+”\”,吐司。长度(长)
.show();
}
公众刷牙{
振动器(
新长{0,200,50,200,50,200,50,200,50,200,50,200,50,
200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, },
-1);
}
公共字符串穿孔机(){
最后的字符串咒骂[]=新字符串[]{“POW!”,“BANG!”,“KERPOW!”,
“噢!”};
返回咒骂词[random.nextInt(咒骂词.length)];
}
}
@Singleton
public class Astroboy {
    // Because Astroboy is a Singleton, we can't directly inject the current Context
    // since the current context may change depending on what activity is using Astroboy
    // at the time.  Instead, inject a Provider of the current context, then we can
    // ask the provider for the context when we need it.
    // Vibrator is bound to context.getSystemService(VIBRATOR_SERVICE) in RoboModule.
    // Random has no special bindings, so Guice will create a new instance for us.
    @Inject
    Provider<Context> contextProvider;
    @Inject
    Vibrator vibrator;
    @Inject
    Random random;

    public void say(String something) {
        // Make a Toast, using the current context as returned by the Context
        // Provider
        Toast.makeText(contextProvider.get(),
                "Astroboy says, \"" + something + "\"", Toast.LENGTH_LONG)
                .show();
    }

    public void brushTeeth() {
        vibrator.vibrate(
                new long[] { 0, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50,
                        200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, },
                -1);
    }

    public String punch() {
        final String expletives[] = new String[] { "POW!", "BANG!", "KERPOW!",
                "OOF!" };
        return expletives[random.nextInt(expletives.length)];
    }
}