Dagger2子注入项为null [英] Dagger2 sub injected item is null

查看:160
本文介绍了Dagger2子注入项为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用dagger2的简单示例



更新



我有一个电机有计算机和水泵类的课程。我这样写,为什么在马达的子注入零件是空的?



这是我的电机类与startEngin方法,检查计算机和waterPump启动。

  public class Motor {

@Inject
public Computer computer;

@Inject
public WaterPump waterPump;


public Motor(){
}

//这里计算机和waterPump为空,不注入
public boolean startEngin() {
if(computer!= null&&& waterPump!= null){
return true;
} else {
return false;
}
}

}

是具有模型名称和电压的计算机类:

  public class Computer {

private int vultage;
私人字符串模型;

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 {

上下文上下文;
String motoName;
String computerName;
String waterPupName;
int电压;

public MotorModule(Context context,String computerName,String waterPupName,int voltage){
this.context = context;
this.waterPupName = waterPupName;
this.computerName = computerName;
this.voltage =电压;
}

@Provides
@Singleton
电机提供电机(){
返回新的Motor();
}

@Provides
@Singleton
计算机提供计算机(){
返回新的计算机(computerName,电压);
}

@Provides
@Singleton
WaterPump提供WaterPump(){
返回新的WaterPump(waterPupName);
}

@Provides
@Singleton
上下文provideContext(){
return this.context;
}

}

这是我的组件类,我知道没有必要getMotor方法。

  @Singleton 
@Component(modules = {MotorModule.class })
public interface MotorComponent {

// Motor getMotor();
void inject(MainActivity activty);

这里的活动注入马达为null:



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(本);

if(motor.startEngin()){

Toast.makeText(this,it started started,Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,motor is not provided,Toast.LENGTH_SHORT).show();
}

}

}

}


解决方案

除了Motor类本身,一切看起来都可以。你用@Inject注释了两个字段(所以Dagger现在知道它可以在那里注入东西),但你从来没有真正要求Dagger使用数据填充这些变量。
您应该明确要求将这些数据填充到代码中的某个位置。
1的方法是通过构造函数注入,这样你的构造函数应该如何看起来像

  public Motor (计算机电脑,WaterPump waterPump){
this.computer =电脑;
this.waterPump = waterPump;
}

...和模块:

  @Provides 
@Singleton
电机提供电机(电脑,水泵水泵){
返回新的电机(电脑,水泵);
}

或者您可以从Motor的constructor中调用dagger实例,并执行以下操作: / p>

  myDaggerInstance.inject(this); 

并记得用@Inject注释注释Motor的构造函数。



使用任何一种方式,您明确地告诉Dagger在某个时间点完成这些依赖关系,所以它们不再是null。干杯!


simple example of using dagger2

UPDATED

I have a Motor class that have Computer and waterpump class. I write them like this by why sub injected parts in Motor is null?

this is my motor class with startEngin method that check computer and waterPump to start.

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;
        }
    }

}

and this is Computer class that have model name and voltage:

public class Computer {

    private int vultage;
    private String model;

    public Computer(String model ,int vultage){

        this.model=model;
        this.vultage = vultage;
    }
}

and this is WaterPump:

public class WaterPump {

    private String name;
    public WaterPump(String name){
        this.name = name;
    }
}

this is my Module:

@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;
    }

}

and this is my component class, I know that there is no need to getMotor method.

@Singleton
@Component(modules = {MotorModule.class})
public interface MotorComponent {

//    Motor getMotor();
    void inject(MainActivity activty);

and here in activity injected motor is null:

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();
        }

    }

}

}

解决方案

Everything looks OK to me, except for Motor class itself. You annotated two fields with @Inject (so Dagger now knows that it can inject stuff there), but you never actually request Dagger to 'fill' those variables with data. You should explicitly request filling those data somewhere in your code. 1 way to do it is through 'constructor injection', so thats how your constructor should look like

public Motor(Computer computer, WaterPump waterPump) {
this.computer = computer;
this.waterPump = waterPump;
}

...and in module:

@Provides
@Singleton
Motor provideMotor(Computer computer, Waterpump waterpump) {
    return new Motor(computer, waterpump);
}

OR you could call dagger instance from your Motor's consctructor and do something like:

myDaggerInstance.inject(this);

and remember to annotate constructor for Motor with @Inject annotation.

Using either way, you explicitly told Dagger to fulfill those dependencies at some point in time, so they won't be null anymore. Cheers!

这篇关于Dagger2子注入项为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆