Dagger 2在Android中注入依赖项错误

时间:2018-08-25 20:40:16

标签: java android dependency-injection dagger-2

我正在使用Dagger 2学习DI,但是我在依赖注入方面遇到了一些问题,我为此应用程序使用了MVP,所以我将所有类都按包分开了。

我正在尝试向演示者注入依赖关系,但出现此错误“如果没有@Provides注释的方法,将无法提供该错误。”,我检查了代码,并检查了一些示例,并假设我正在注入这种依赖关系,我已经尝试了不同的方式,但是无法解决此错误,而且我也不知道如何解决它。

我只想打开第二个从演示者SplashActivity调用的活动,而是通过DI注入。

Gradle中的库

implementation 'com.google.dagger:dagger:2.17'
annotationProcessor 'com.google.dagger:dagger-compiler:2.17'

MovieApplication.java

    public class MovieApplication extends Application {

    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        setupGraph();
    }

    private void setupGraph() {
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    public static MovieApplication get(Context context){
        return (MovieApplication) context.getApplicationContext();
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }
}

AppComponent.java

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    @ApplicationContext
    Context context();

}

AppModule.java

@Module
public class AppModule {

    private MovieApplication mMovieApplication;

    public AppModule(MovieApplication movieApplication) {
        this.mMovieApplication = movieApplication;
    }

    @Provides
    @ApplicationContext
    Context provideContext(){
        return mMovieApplication;
    }

    @Provides
    Application provideApplication(){
        return mMovieApplication;
    }
}

MovieComponent.java

@MovieScreenScope
@Component(dependencies = AppComponent.class, modules = MovieModule.class)
public interface MovieComponent {

    void inject(SplashActivity splashActivity);
}

MovieModule.java

@Module
public class MovieModule {

    private Activity mActivity;

    public MovieModule(Activity activity) {
        this.mActivity = activity;
    }

    @Provides
    @ActivityContext
    Context provideContext(){
        return mActivity;
    }

    @Provides
    Activity provideActivity(){
        return mActivity;
    }

    @Provides
    @MovieScreenScope
    SplashPresenter<SplashView> provideSplashPresenter(SplashPresenterImpl<SplashView> presenter){
        return presenter;
    }

}

SplashActivity.java

public class SplashActivity extends BaseActivity implements SplashView {

    @Inject
    SplashPresenter<SplashView> presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMovieComponent().inject(this);
        presenter.openLoginActivity();
    }

    @Override
    public void openLoginActivity() {
        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        finish();

    }

    @Override
    public void openHomeActivity() {
        startActivity(new Intent(SplashActivity.this, ContainerActivity.class));
        finish();
    }
}

BaseActivity.java

public class BaseActivity extends AppCompatActivity {

    private MovieComponent movieComponent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupGraph();
    }

    private void setupGraph() {
        movieComponent = DaggerMovieComponent.builder()
                .movieModule(new MovieModule(this))
                .appComponent(MovieApplication.get(this).getAppComponent())
                .build();
    }

    public MovieComponent getMovieComponent() {
        return movieComponent;
    }
}

SplashView.java

public interface SplashView {

    void openLoginActivity();
    void openHomeActivity();
}

SplashPresenter.java

public class SplashPresenterImpl<V extends SplashView> implements SplashPresenter<V> {

    private SplashView splashView;

    @Inject
    public SplashPresenterImpl(V splashView) {
        this.splashView = splashView;
    }

    @Override
    public void openLoginActivity() {
        splashView.openLoginActivity();

    }

    @Override
    public void openMainActivity() {

    }

    @Override
    public void verifyInternetConection() {

    }

    @Override
    public void getMoviesData() {

    }

}

SplashPresenter.java

@MovieScreenScope
public interface SplashPresenter <V extends SplashView> {

    void openLoginActivity();
    void openMainActivity();
    void verifyInternetConection();
    void getMoviesData();
}

1 个答案:

答案 0 :(得分:0)

这里唯一的问题似乎是splashView类的构造函数中的SplashPresenterImpl参数。用匕首创建SplashPresenterImpl的对象时,它会尝试找到返回@Provides的{​​{1}}方法。基本上,它试图在依赖关系图中找到SplashView,并且由于不存在用SplashView注释的方法来返回此对象,因此会出现此错误。