简体   繁体   English

Dagger2子注入项为空

[英]Dagger2 sub injected item is null

simple example of using dagger2 使用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? 我之所以这样写它们,是因为为什么Motor中的子注入零件为空?

this is my motor class with startEngin method that check computer and waterPump to start. 这是我的带有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;
        }
    }

}

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: 这是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. 这是我的组件类,我知道不需要getMotor方法。

@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. 您用@Inject注释了两个字段(因此Dagger现在知道它可以在其中注入内容),但是您从未真正要求Dagger用数据“填充”这些变量。 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: 或者,您可以从Motor的构造器调用dagger实例,然后执行以下操作:

myDaggerInstance.inject(this);

and remember to annotate constructor for Motor with @Inject annotation. 并记得使用@Inject注释为Motor的构造函数进行注释。

Using either way, you explicitly told Dagger to fulfill those dependencies at some point in time, so they won't be null anymore. 无论使用哪种方式,您都明确地告诉Dagger在某个时间点实现这些依赖关系,因此它们不再为null。 Cheers! 干杯!

You need to save reference to MotorComponent instance and use it to inject into Activtiy and Motor classes. 您需要保存对MotorComponent实例的引用,并将其用于注入Activtiy和Motor类。 Add void inject(Motor m) into you component, and call component.inject(myMotor), or use constructor injection on Motor class instead. 将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();
}

And

public class Motor {

    @Inject
    public Computer computer;

    @Inject
    public WaterPump waterPump;


    public Motor(){
    }

Should be one of the following: 应为以下之一:

1.) 1.)

@Singleton
public class Motor {
    @Inject
    public Computer computer;

    @Inject
    public WaterPump waterPump;

    @Inject
    public Motor() {
    }

And

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

Or 要么

2.) 2.)

public class Motor {
    public Computer computer;

    public WaterPump waterPump;

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

And

@Provides
@Singleton
Motor provideMotor(Computer computer, WaterPump waterPump) {
    return new Motor(computer, waterPump);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM