我是Dagger2的新手。 我在我的活动中注入Presenter有问题 我在阅读本文https://android.jlelse.eu/inject-interfaces-without-providing-in-dagger-2-618cce9b1e29之后尝试解决我的问题,但这并没有帮助我。我希望有人帮助我,我花了一整天时间来解决它...
这是我的模块:
@Module
class AppModule(private val appContext: Context) {
@Singleton
@Provides
internal fun provideContext(): Context {
return appContext
}
@Singleton
@Provides
internal fun providePreferences(): SharedPreferences {
return appContext.getSharedPreferences(
appContext.resources.getString(R.string.shared_preferences_name), Context.MODE_PRIVATE)
}
}
@Module
abstract class ActivityModule {
@Binds
abstract fun provideMakeCheckPresenter (p :
MakeCheckPresenter<MakeCheckMvpView>)
: MakeCheckMvpPresenter<MakeCheckMvpView>
}
这是我的组件:
@Component(modules = { AppModule.class, ActivityModule.class})
@Singleton
public interface AppComponent {
void inject(MakeCheckActivity makeCheckActivity);
}
这是我的App类:
class App : android.support.multidex.MultiDexApplication() {
override fun onCreate() {
super.onCreate()
component = buildComponent()
}
protected fun buildComponent(): AppComponent {
return DaggerAppComponent.builder().appModule(AppModule(this)).build()
}
companion object {
var component: AppComponent? = null
private set
}
}
这是我的活动:
class MakeCheckActivity : BaseActivity(), MakeCheckMvpView {
@Inject lateinit var presenter: MakeCheckMvpPresenter<MakeCheckMvpView>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.new_check_activity)
App.getComponent().inject(this)
}
}
这是我的主持人:
class MakeCheckPresenter<V : MakeCheckMvpView>
@Inject constructor() : BasePresenter<MakeCheckMvpView>(), MakeCheckMvpPresenter<MakeCheckMvpView> {
override fun saveEnterpriseId(enterpriseId: Int) {
//model.enterpriseId
}
}
这里是演示者和视图的接口:
interface MakeCheckMvpView : MvpView {
}
interface MakeCheckMvpPresenter<in V : MakeCheckMvpView> : MvpPresenter<V> {
fun saveEnterpriseId(enterpriseId : Int)
}
以及这里的基类:
open class BasePresenter<V : MvpView> @Inject constructor(): MvpPresenter<V> {
}
abstract class BaseActivity : AppCompatActivity(), MvpView{
}
总是当我建立时我有这个错误:
...\di\component\AppComponent.java:62: error: ..ui.check_making.MakeCheckMvpPresenter<? super ...ui.check_making.MakeCheckMvpView> cannot be provided without an @Provides- or @Produces-annotated method.
e:
e: void inject(MakeCheckActivity makeCheckActivity);
我该如何解决?