Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 error: android.content.Context cannot be provided without an @Provides-annotated method

I am working on implementing dagger 2 in my project. For this, i have written below lines of code:

@Inject
VideoControllerView mediaController;

@Module
public class PlayerModule {

    @Provides
    VideoControllerView providesVideoControllerView(Context context, VideoControllerView.Controlers cntrls) {
        return new VideoControllerView(context, cntrls);
    }
}


@Component(modules = PlayerModule.class)
public interface PlayerComponent {
    VideoControllerView getVideoControllerView();
}

But when i am trying to compile my app, i am getting below given error:

Error:(14, 25) error: android.content.Context cannot be provided without an @Provides-annotated method.
android.content.Context is injected at
com.multitv.multitvplayersdk.module.PlayerModule.providesVideoControllerView(context, …)
com.multitv.multitvplayersdk.controls.VideoControllerView is provided at
com.multitv.multitvplayersdk.component.PlayerComponent.getVideoControllerView()

I have looked around on how to resolve this issue but of no avail. Please help me.

like image 204
Naseeb Sheoran Avatar asked Jun 05 '17 06:06

Naseeb Sheoran


1 Answers

Please try to understand the error.

PlayerModule.providesVideoControllerView(context, …) requires a context, but Dagger does not have access to any Context object, so it cannot pass in any Context. It thus creates the error, telling you that it can't find any Context and that you should add a way for it to do so.

Context cannot be provided without an @Provides-annotated method.

Hence...A context cannot be provided without a @Provides-annotated method.


Make sure that the component your PlayerModule is a part of has access to a context.

Either make it a subcomponent of your ApplicationComponent, add a Context to your PlayerModule, bind your Activity instance as a Context, etc

There's a lot of ways to do this, but in the end you require a method like the following in one of your modules:

@Provides Context provideContext() { return myContext; }

Once Dagger can find a Context the error will go away.

like image 98
David Medenjak Avatar answered Sep 18 '22 12:09

David Medenjak