kotlin.jvm.functions.Function1 <?如果没有@Provides注释的方法,则无法提供超级java.lang.String,kotlin.Unit>

时间:2019-11-04 09:53:57

标签: android dagger-2 dagger

用法/注射剂:

import java.util.Arrays;

class Example {
    void method(Object... args) {
        System.out.println(Arrays.toString(args));
    }
    public static void main(String[] args) {
        Example e = new Example();
        e.method(1, "foo", 2, "bar", 3);
        e.method(true, "foo", 'c', 'd', new Boolean(false), false);
    }
}

我的模块

    @Inject
    @field:Named("MyStringSaverFunction")
    lateinit var stringSaverFunction: (String) -> Unit

错误详细信息:

@Module(includes = [AbcModule.UiBinding::class])
object AbcModule {
    @Module
    interface UiBinding {
        @PerFragment
        @ContributesAndroidInjector
        fun provideFragment(): AbcFragment
    }
    @Provides
    @JvmStatic
    @Singleton
    @Named("AbcSharedPrefs")
    fun provideAbcSharedPrefs(context: Context) : SharedPreferences {
        return context.getSharedPreferences("AbcSharedPrefs", Context.MODE_PRIVATE)
    }

    @Provides
    @JvmStatic
    @Singleton
    @Named("MyStringSaverFunction")
    fun provideDfIntentSaver(@Named("AbcSharedPrefs") sharedPreferences: SharedPreferences): (String) -> Unit {
        return { theString: String ->
            sharedPreferences.edit().putString("TheKey", theString).apply()
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在注入位置使用@JvmSuppressWildcards来匹配签名-

@Inject
@field:Named("MyStringSaverFunction")
@JvmSuppressWildcards
lateinit var stringSaverFunction: (String) -> Unit