简体   繁体   English

没有@Provides注释的方法无法提供

[英]Cannot be Provided Without an @Provides-annotated Method

There have been many other similar questions, but none of the answers have been applicable to my code. 还有许多其他类似的问题,但是没有一个答案适用于我的代码。 I cannot figure out what I have done wrong. 我无法弄清楚我做错了什么。

First I have a NetworkModule that is used as a module for the ApplicationComponent : 首先,我有一个NetworkModule用作ApplicationComponent的模块:

@Module
open class NetworkModule {

    companion object {
        private val BASE = "http://www.example.com/"
    }

    @Provides @ApplicationScope
    fun provideClient(): OkHttpClient = OkHttpClient()

    @Provides @ApplicationScope
    fun provideMoshi(): Moshi {
        return Moshi.Builder().add(InstantAdapter).add(UriAdapter).build()
    }

    @Provides @ApplicationScope
    fun provideRetrofit(client: OkHttpClient, moshi: Moshi): Retrofit {
        return Retrofit.Builder().client(client).baseUrl(BASE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .build()
    }

    @Provides @ApplicationScope
    fun provideArticleService(retrofit: Retrofit): ArticleService {
        return retrofit.create(ArticleService::class.java)
    }

}

@ApplicationScope @Component(modules = arrayOf(ContextModule::class, RealmModule::class, NetworkModule::class))
interface ApplicationComponent {}

Then the ApplicationComponent is built in my Application class: 然后ApplicationComponent是建立在我的Application类:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        AndroidThreeTen.init(this)

        plantLog()
        drawDagger()
    }

    private fun drawDagger() {
        Injector.initializeApplicationComponent(this)
    }

    // ...

}

object Injector {

    lateinit var applicationComponent: ApplicationComponent
        private set

    fun initializeApplicationComponent(context: Context) {
        applicationComponent = DaggerApplicationComponent.builder()
                .contextModule(ContextModule(context))
                .networkModule(NetworkModule())
                .realmModule(RealmModule())
                .build()
    }

    // ...

}

Then I have an ActivityModule that is used in the ActivityComponent (which has ApplicationComponent as a dependency): 然后我有一个ActivityModule是在所使用的ActivityComponent (其具有ApplicationComponent作为依赖):

@Module
open class ActivityModule(private val activity: AppCompatActivity) {

    @Provides @ActivityScope @ActivityContext
    fun provideContext(): Context = activity

    @Provides @ActivityScope
    fun provideFragmentManager(): FragmentManager = activity.supportFragmentManager

}

@ActivityScope @Component(dependencies = arrayOf(ApplicationComponent::class), modules = arrayOf(ActivityModule::class))
interface ActivityComponent {

    fun inject(activity: MainActivity)

}

Finally, I create a new ActivityComponent in the MainActivity and @Inject the ArticleService : 最后,我在MainActivity创建一个新的ActivityComponent ,并@Inject the ArticleService

class MainActivity : AppCompatActivity() {

    @Inject lateinit var service: ArticleService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        DaggerActivityComponent.builder()
                .applicationComponent(Injector.applicationComponent)
                .activityModule(ActivityModule(this))
                .build().inject(this)

        service.getNewsArticles()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(
                        { response -> onNext(response) },
                        { error -> onError(error) })
    }

    // ...

}

But when I try to build I get the following error, even though I believe the provideArticleService() function in NetworkModule is annotated correctly: 但是,当我尝试构建时,即使我相信NetworkModuleprovideArticleService()函数已正确标注,也会出现以下错误:

ArticleService cannot be provided without an @Provides- or @Produces-annotated method. 没有@Provides或@Produces批注的方法就无法提供ArticleService。

You're missing the provision methods to inherit to your Activity scoped component. 您缺少了继承到Activity范围内的组件的置备方法。 Either use subcomponents instead of component dependency, or define the provision methods in your application component. 使用子组件代替组件依赖关系,或者在应用程序组件中定义供应方法。

 @ApplicationScope @Component(modules = arrayOf(ContextModule::class, RealmModule::class, NetworkModule::class))
 interface ApplicationComponent {
      ArticleService articleService();
 }

暂无
暂无

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

相关问题 如果没有@Provides 注释的方法 Dagger/MissingBinding,则无法提供 - cannot be provided without an @Provides-annotated method Dagger/MissingBinding 没有@Annex-annotated方法就无法提供上下文,但它是? - Context cannot be provided without an @Provides-annotated method, but it is? Android匕首2:如果没有@Provides注释的方法,则无法提供依赖项 - Android dagger 2: dependency cannot be provided without an @Provides-annotated method 不能在没有 @Provides 注释的方法的情况下提供(Android Kotlin 中的 Dagger 2) - Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin) Dagger 2错误:依赖项“如果没有@Provides注释的方法就无法提供” - Dagger 2 error: dependency “cannot be provided without an @Provides-annotated method" 错误:如果没有 @Provides-annotated 方法就无法提供 - Error: cannot be provided without an @Provides-annotated method 匕首柄:如果没有@Provides-annotated 方法,则无法提供 - Dagger Hilt: cannot be provided without an @Provides-annotated method 没有@Provides注释的方法就无法提供对象 - Objects cannot be provided without an @Provides-annotated method 没有@Provides注释的方法,无法提供该类 - The class cannot be provided without an @Provides-annotated method 不能在没有 @Inject 构造函数或从 @Provides-annotated 方法的情况下提供 - cannot be provided without an @Inject constructor or from an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM