[英]Dagger2 sub injected item is null
使用dagger2的簡單示例
更新
我有一個馬達班,有計算機班和水泵班。 我之所以這樣寫它們,是因為為什么Motor中的子注入零件為空?
這是我的帶有startEngin方法的馬達類,該方法檢查計算機和waterPump以啟動。
public class Motor {
@Inject
public Computer computer;
@Inject
public WaterPump waterPump;
public Motor(){
}
// here computer and waterPump are null and not injected
public boolean startEngin(){
if(computer!=null && waterPump!=null){
return true;
}else{
return false;
}
}
}
這是具有型號名稱和電壓的計算機類:
public class Computer {
private int vultage;
private String model;
public Computer(String model ,int vultage){
this.model=model;
this.vultage = vultage;
}
}
這是WaterPump:
public class WaterPump {
private String name;
public WaterPump(String name){
this.name = name;
}
}
這是我的模塊:
@Module
public class MotorModule {
Context context;
String motoName;
String computerName;
String waterPupName;
int voltage;
public MotorModule(Context context, String computerName, String waterPupName, int voltage) {
this.context = context;
this.waterPupName = waterPupName;
this.computerName = computerName;
this.voltage = voltage;
}
@Provides
@Singleton
Motor provideMotor() {
return new Motor();
}
@Provides
@Singleton
Computer provideComputer() {
return new Computer(computerName, voltage);
}
@Provides
@Singleton
WaterPump provideWaterPump() {
return new WaterPump(waterPupName);
}
@Provides
@Singleton
Context provideContext() {
return this.context;
}
}
這是我的組件類,我知道不需要getMotor方法。
@Singleton
@Component(modules = {MotorModule.class})
public interface MotorComponent {
// Motor getMotor();
void inject(MainActivity activty);
在這里,在活動注入的電機中為空:
public class MainActivity extends AppCompatActivity {
@Inject
public Motor motor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerMotorComponent.builder().motorModule(new MotorModule
(this, "mahdi'PC", "my " +
"Water pump", 12)).build().inject(this);
if (motor.startEngin()) {
Toast.makeText(this, "it is started", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "motor is not provided", Toast.LENGTH_SHORT).show();
}
}
}
}
對我來說,一切看起來都不錯,除了賽車課本身。 您用@Inject注釋了兩個字段(因此Dagger現在知道它可以在其中注入內容),但是您從未真正要求Dagger用數據“填充”這些變量。 您應該明確地要求在代碼中的某處填充這些數據。 一種方法是通過“構造函數注入”,這就是構造函數的外觀
public Motor(Computer computer, WaterPump waterPump) {
this.computer = computer;
this.waterPump = waterPump;
}
...並在模塊中:
@Provides
@Singleton
Motor provideMotor(Computer computer, Waterpump waterpump) {
return new Motor(computer, waterpump);
}
或者,您可以從Motor的構造器調用dagger實例,然后執行以下操作:
myDaggerInstance.inject(this);
並記得使用@Inject注釋為Motor的構造函數進行注釋。
無論使用哪種方式,您都明確地告訴Dagger在某個時間點實現這些依賴關系,因此它們不再為null。 干杯!
您需要保存對MotorComponent實例的引用,並將其用於注入Activtiy和Motor類。 將void inject(Motor m)添加到您的組件中,然后調用component.inject(myMotor),或者改為對Motor類使用構造函數注入。
public class Motor {
Computer computer;
WaterPump waterPump;
public Motor( Computer computer, WaterPump waterPump){
this.computer = computer;
this.waterPump = waterPump;
}
// here computer and waterPump are null and not injected
public boolean startEngin(){
if(computer!=null && waterPump!=null){
return true;
}else{
return false;
}
}
}
/** Add method, providing Motor class instance - it will use another
.provide() methods from this component to get computer and waterpump objects
(Costtructor injection)
*/
/**
* Arguments is provided by DI
* @param computer
* @param waterPump
* @return
*/
@Provides
@Singleton
Motor provideMotors(Computer computer, WaterPump waterPump) {
Motor motor = new Motor(computer, waterPump);
return motor
}
/** Store reference to your component (better do it in Application) */
MotorComponent component = DaggerMotorComponent.builder().motorModule(new MotorModule
(this, "mahdi'PC", "my " +
"Water pump", 12)).build();
// inject into Activity
componen.inhject(this);
@Provides
@Singleton
Motor provideMotor() {
return new Motor();
}
和
public class Motor {
@Inject
public Computer computer;
@Inject
public WaterPump waterPump;
public Motor(){
}
應為以下之一:
1.)
@Singleton
public class Motor {
@Inject
public Computer computer;
@Inject
public WaterPump waterPump;
@Inject
public Motor() {
}
和
public MotorModule(Context context, String computerName, String waterPupName, int voltage) {
this.context = context;
this.waterPupName = waterPupName;
this.computerName = computerName;
this.voltage = voltage;
}
//@Provides
//@Singleton
//Motor provideMotor() {
// return new Motor();
//}
要么
2.)
public class Motor {
public Computer computer;
public WaterPump waterPump;
public Motor(Computer computer, WaterPump waterPump) {
this.computer = computer;
this.waterPump = waterPump;
}
和
@Provides
@Singleton
Motor provideMotor(Computer computer, WaterPump waterPump) {
return new Motor(computer, waterPump);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.