Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger2 Cannot be provided without an @Provides- or @Produces-annotated method

So i'm developing an app with mvp pattern and i'm having issues using Dagger2

I have created an interface for my presenter called HelloPresenter.

And then i created an implementation for that interface called HelloPresenterImpl

My HelloPresenter

public interface HelloPresenter {

    public void sayHello();

}

My HelloPresenterImpl

public class HelloPresenterImpl implements HelloPresenter {

    StoryView storyView;

    @Inject
    public HelloPresenterImpl(StoryView storyView) {
        this.storyView = storyView;
    }

    @Override
    public void sayHello() {
        Log.d(TAG, "hello there");
    }

}

My component

@UserScope
@Component(dependencies = NetComponent.class, modules = RetrofitModule.class)
public interface StoryComponent {
    void inject(MainActivity activity);
    void inject(HelloPresenter helloPresenter);
}

My module

@Module
public class HelloModule {

    @Provides
    public HelloPresenter providesHelloPresenter(final HelloPresenterImpl presenter){
        return presenter;
    }

}

My BaseApplication

public class BaseApplication extends Application {

    private NetComponent mNetComponent;
    private StoryComponent mStoryComponent;

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

        mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this))
                .netModule(new NetModule())
                .build();

        mStoryComponent = DaggerStoryComponent.builder()
                .netComponent(mNetComponent)
                .retrofitModule(new RetrofitModule())
                .storyModule(new StoryModule())
                .build();

    }

    public NetComponent getNetComponent() {
        return mNetComponent;
    }

    public StoryComponent getStoryComponent() {
        return mStoryComponent;
    }

Now, when i try to inject it in my MainActivity, it throws an Error saying

Error:(18, 10) error: com.exampleapp.Hello.presenter.HelloPresenter cannot be provided without an @Provides- or @Produces-annotated method. com.exampleapp.Hello.presenter.HelloPresenter is injected at com.exampleapp.Hello.MainActivity.HelloPresenter com.exampleapp.Hello.MainActivity is injected at com.exampleapp.di.components.HelloComponent.inject(activity)

public class MainActivity extends AppCompatActivity {

    @Inject
    SharedPreferences mSharedPreferences;

    @Inject
    StoryInterface storyInterface;

    @Inject 
    HelloPresenter helloPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((BaseApplication) getApplication()).getStoryComponent().inject(this);

        helloPresenter.sayHello();  // This throws an Error

    }

Please can someone point me in the right direction? Thanks

like image 554
Tosin Onikute Avatar asked Mar 13 '17 00:03

Tosin Onikute


1 Answers

Without seeing the whole project, my guess is Dagger can't figure how to create HelloPresenterImpl instance because you don't provide StoryView. I think you'll need:

@Provides
public StoryView providesStoryView() {
    return new StoryView();
}
like image 127
pt2121 Avatar answered Sep 29 '22 11:09

pt2121