简体   繁体   English

Dagger2没有注入领域

[英]Dagger2 is not injecting field

I have this module: 我有这个模块:

@Module
public class MainModule {

    private Context context;

    public MainModule(Context context) {
        this.context = context;
    }

    @Provides
    @Singleton
    Dao providesDao() {
        return new Dao();
    }

    @Provides
    @Singleton
    FirstController providesFirstController(Dao dao) {
        return new FirstController(dao);
    }

    @Provides
    @Singleton
    SecondController providesSecondController(Dao dao) {
        return new SecondController(dao);
    }

}

and this component: 和这个组件:

@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {

    void inject(FirstView view);

    void inject(SecondView view);

}

and, finally, the injector class, that is initialized in App.onCreate() method: 最后是在App.onCreate()方法中初始化的喷射器类:

public enum Injector {

    INSTANCE;

    MainComponent mainComponent;

    public void initialize(App app) {
        mainComponent = DaggerMainComponent.builder()
                .mainModule(new MainModule(app))
                .build();
    }

    public MainComponent getMainComponent() {
        return mainComponent;
    }
}

In my FirstView and SecondView (that are Fragment s), I have this: 在我的FirstView和SecondView(即Fragment )中,我有以下内容:

    @Inject
    FirstController controller; //and SecondController for the second view

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Injector.INSTANCE.getMainComponent().inject(this);
    }

In the first fragment, everything is ok, the controller is injected. 在第一个片段中,一切正常,注入了控制器。 But in the second view it is not: just returning null . 但是在第二种视图中,它不是:只返回null

I have put breakpoints in "provides" module's methods and the providesFirstController is executed but not the providesSecondController . 我已经把断点在“规定”模块的方法和providesFirstController执行,但不是providesSecondController

What am I doing wrong? 我究竟做错了什么? I'm new in Dagger2 so any advice will be appreciated. 我是Dagger2的新手,因此任何建议将不胜感激。

If those are Fragments try moving the code connected with injecting: 如果这些是Fragments尝试移动与注入相关的代码:

Injector.INSTANCE.getMainComponent().inject(this);

to the Fragment 's public void onCreate(Bundle savedInstanceState) method. Fragmentpublic void onCreate(Bundle savedInstanceState)方法。 If the views are not visible on the screen at the same time (added by FragmentManager ), then the SecondView onAttach method may not be called. 如果视图同时在屏幕上不可见(由FragmentManager添加),则可能不会调用SecondView onAttach方法。

Solved! 解决了! I have change the signature of inject methods to: inject方法的签名更改为:

@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {

    void inject(FirstFragment view);

    void inject(SecondFragment view);

}

I forget to say that FirstView and SecondView are interface s, not classes. 我忘了说FirstView和SecondView是interface ,而不是类。 And inject methods need the concrete class. 而注入方法则需要具体的类。

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

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