简体   繁体   English

如何在android中的MVP层之间使用注入?

[英]how to use injection between MVP layers in android?

I have been using using dagger 2 in my project lately,我最近一直在我的项目中使用Dagger 2

the problem is when I try to build my project, the presenter in my login activity which injected like below is null ,问题是当我尝试构建我的项目时,我的登录活动中注入的演示者如下所示为 null

and when I try to build the project当我尝试构建项目时

presenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method...如果没有@Inject 构造函数或来自@Provides- 或@Produces-注释的方法,则无法提供presenter...

I don't understand what have I done wrong??, please someone help me with this,我不明白我做错了什么??请有人帮我解决这个问题,

thanks in advance.提前致谢。

Here is my Login Activity, the presenter here is null, which shows that, I've not injected it properly这是我的登录活动,这里的演示者为空,这表明我没有正确注入它

@Inject
LoginPresenter presenter;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    InjectHelper.getRootComponent().injectPresenter(this);
    presenter.setProgressBarVisiblity(View.INVISIBLE);
}

this is my presenter module这是我的演示者模块

 @Module
 public class PresenterModule {
 private final LoginActivity activity;

public PresenterModule(LoginActivity activity) {
    this.activity = activity;
}

@Provides
@Singleton
public LoginActivity providesView() {

    return activity;
}
}


@Provides
@Singleton
public LoginPresenter providesPresenter()
{
    return new LoginPresenter();
}
}

this inject helper class这个注入助手类

public class InjectHelper {
  private static RootComponent sRootComponent;

static {
    initModules();
}

private static void initModules() {
    sRootComponent = getRootComponentBuilder().build();
}

public static DaggerRootComponent.Builder getRootComponentBuilder() {
    return DaggerRootComponent.builder();
}

public static RootComponent getRootComponent() {
    if (sRootComponent == null) {
        initModules();
    }
    return sRootComponent;
}

}

this is root component class这是根组件类

@Singleton
@Component(modules = {
     PresenterModule.class
})
public interface RootComponent {
void injectLoginView(LoginPresenter loginPresenter);
}

you need to inform the dagger which views want to use injection, in your component.您需要在组件中通知匕首哪些视图要使用注入。 you must change the inject function code in your component to below:您必须将组件中的注入功能代码更改为以下内容:

    void inject(LoginActivity activity);

for showing dagger what you want, you need to use @dagger annotation NOT by sending it as inject function in component file.为了显示你想要的匕首,你需要使用@dagger 注释,而不是通过在组件文件中将其作为注入函数发送。 as you did properly:正如你所做的那样:

@Inject
LoginPresenter presenter;

Dagger will search for a variable of type LoginPresenter in your module and finds the proper provider method using the type. Dagger 将在您的模块中搜索类型为 LoginPresenter 的变量,并使用该类型找到正确的提供程序方法。

what you put in your component as a argument for "inject" function tells the Dagger what view you are going to do injection in (NOT what you want to inject)您在组件中作为“注入”函数参数的内容告诉 Dagger 您将要注入的视图(而不是您想要注入的视图)

@Singleton
@Component(modules = {PresenterModule.class})
public interface RootComponent {
    void inject(LoginActivity activity);
}

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

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