注入活动时,Presenter为null

时间:2018-03-15 04:17:53

标签: android dagger-2 android-mvp

我正在尝试使用dagger2并希望将一个演示者注入活动中,我在互联网上搜索了为什么演示者为空但后来我得到了不同的实现注入一个活动与几个模块。在尝试创建匕首依赖项时,有人可以帮我理解我出错的地方吗?

我定义了以下类:

ActivityComponent.class

@PerActivity
@Component(modules = {ActivityModule.class}, dependencies = {AppComponent.class, GitHubComponent.class})
public interface ActivityComponent {
    void inject(AppCompatActivity activity);
}

ActivityModule.class

@Module
public class ActivityModule {
    private AppCompatActivity activity;

    public ActivityModule(AppCompatActivity activity) {
        this.activity = activity;
    }

    @Provides
    AppCompatActivity provideActivity() {
        return activity;
    }

    @Provides
    @PerActivity public MainView provideMainView() {
        return new MainViewImpl(activity);
    }

    @Provides
    @ActivityScope
    Context providesContext() {
        return activity;
    }

}

AppModule.class

@Singleton
@Module
public class AppModule {
    private final GitHubApp application;

    public AppModule(final GitHubApp application) {
        this.application = application;
    }

    @Provides
    Application provideApplication() {
        return application;
    }

    @Provides
    public GitHubLog provideGithubLog() {
        return new GitHubLog();
    }
}

GitHubModule.class

@Module
public class GitHubModule {
    @Provides
    public MainInteractor provideMainInteractor() {
        return new MainInteractorImpl();
    }

    @Provides
    @PerActivity
    public MainPresenter provideMainPresenter(){
        return new MainPresenterImpl();
    }
}

AppComponent.class

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
    void inject(GitHubApp gitHubApp);

    void inject(GitHubLog gitHubLog);

    Application application();

    GitHubLog getGitHubLog();
}

GithubComponent.class

@Component(modules = {GitHubModule.class, AppModule.class})
public interface GitHubComponent {
    void inject(MainPresenterImpl presenter);
}

在应用程序类中,我创建了一个appcomponent实例和githubcomponent实例,我在BaseActivity中使用它来创建activitycomponent

我在MainAcitivity that extends BaseActivity内注入了演示者,我得到一个空指针异常,说presenter为null。

我的实施不正确吗?我能错过什么?

修改 GithubApp.class

public class GitHubApp extends Application {
    public static GitHubApp INSTANCE;
    private AppComponent appComponent;
    private GitHubComponent gitHubComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        getAppComponent().inject(this);
    }

    public AppComponent getAppComponent() {
        if (appComponent == null) {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(this))
                    .gitHubModule(new GitHubModule())
                    .build();
        }

        return appComponent;
    }

    public GitHubComponent getGitHubComponent() {
        if (gitHubComponent == null) {
            gitHubComponent = DaggerGitHubComponent.builder()
                    .gitHubModule(new GitHubModule())
                    .build();
        }
        return gitHubComponent;
    }
}

我如何将演示者注入活动如下: BaseActivity.class有一个返回活动组件

的方法
return DaggerActivityComponent.builder()
            .appComponent(((GitHubApp)getApplication()).getAppComponent())
            .gitHubComponent(((GitHubApp)getApplication()).getGitHubComponent())
            .activityModule(new ActivityModule(this))
            .build();

在MainActivity.class中,我使用它: 在调用super.onCreate()之前,请致电getActivityComponent().inject(this);

@注入 MainPresenter mainPresenter是变量声明

EDIT2:

Chisko和Muhammad Babar建议的更改合作,,因为它还需要更改 inject(AppCompatActivity activity)inject(MainActivity activity)

2 个答案:

答案 0 :(得分:1)

除了评论之外,我认为您还缺少其他设置:

<强> GitHubApp.java

GitHubAppComponent component;

// call this inside onCreate()
private void setupComponent() {
    component = DaggerGitHubAppComponent.builder()
        .gitHubModule(new GitHubModule(this))
        .build();
}

public GitHubAppComponent getComponent() {
     return component;
}

除了活动组件,它需要在您创建的BaseActivity上使用抽象方法:

<强> BaseActivity.java

abstract void setupComponent(GitHubAppComponent appComponent);

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    GitHubApp app = (GitHubApp) getApplication();
    GutHubAppComponent component = app.getComponent();
    setupComponent(component);
}

然后,您的活动还应创建组件:

<强> MainActivity.java

@Override
protected void setupComponent(GitHubAppComponent appComponent) {
    ActivityComponent component = DaggerMainActivityComponent.builder()
        .gitHubAppComponent(appComponent)
        .activityModule(new ActivityModule(this))
        .build()
    component.inject(this);
}

我知道这可能看起来像a $$中的痛苦,但是一旦你掌握了Dagger,你就会发现它是多么简单和方便。

答案 1 :(得分:1)

ActivityComponent 中,将void inject(AppCompatActivity activity)替换为void inject(MainActivity activity)