您好我正在尝试使用dagger 2.11注入LinearLayout,我收到以下错误:
Error:(12, 2) error: [dagger.android.AndroidInjector.inject(T)] android.support.v7.widget.LinearLayoutManager cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
public abstract interface ApplicationComponent extends dagger.android.AndroidInjector<dagger.android.DaggerApplication> {
^
android.support.v7.widget.LinearLayoutManager is injected at
com.MyClass.linearLayoutManager
com.MyClass is injected at
dagger.android.AndroidInjector.inject(arg0)
这是我的应用程序模块
@Module
companion object {
@Provides
@Singleton
@JvmStatic
fun provideApplicationContext(application: MyApplication): Context = application
@Provides
@Singleton
@JvmStatic
fun provideSharedPreference(application: MyApplication): SharedPreferences =
application.getSharedPreferences(PIANO_PREFERENCE_NAME, Context.MODE_PRIVATE)
@Provides
@Singleton
@JvmStatic
fun provideInputManagerService(application: MyApplication): InputMethodManager =
application.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
//below doesnt work but the above works fine
@Provides
@Singleton
@JvmStatic
@Named(DagConstants.HORIZONTAL_LAYOUT_MANAGER)
fun provideVerticalLinearLayout(application: MyApplication): LinearLayoutManager =
LinearLayoutManager(application, LinearLayoutManager.VERTICAL, false)
@Provides
@Singleton
@JvmStatic
@Named(DagConstants.VERTICAL_LAYOUT_MANAGER)
fun provideHorizontalLinearLayout(application: MyApplication): LinearLayoutManager =
LinearLayoutManager(application, LinearLayoutManager.HORIZONTAL, false)
}
MyClass的:
@Inject
@Named(DagConstants.VERTICAL_LAYOUT_MANAGER)
lateinit var linearLayoutManager: LinearLayoutManager
无法注入linearLayout但其他所有内容都已注入并正常工作
答案 0 :(得分:1)
您忘记使用@field:Named
@Inject
@field:Named(DagConstants.VERTICAL_LAYOUT_MANAGER)
lateinit var linearLayoutManager: LinearLayoutManager