我正在尝试使用匕首2.21建立一个项目,这些是我的课程
BaseApp
class BaseApp : DaggerApplication() {
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.builder().create(this)
}
}
应用组件
@Singleton
@Component(
modules = [
AndroidSupportInjectionModule::class,
AppModule::class,
NetworkModule::class,
ActivityBuilder::class]
)
interface AppComponent: AndroidInjector<BaseApp> {
@Component.Builder
abstract class Builder : AndroidInjector.Builder<BaseApp>() {}
}
基本活动
abstract class BaseActivity: DaggerAppCompatActivity(), BaseFragment.Callback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onFragmentAttached() {
}
override fun onFragmentDetached(tag: String) {
}
fun isNetworkConnected(): Boolean {
return NetManager(applicationContext).isConnectedToInternet
}
}
基本片段
abstract class BaseFragment: DaggerFragment() {
abstract fun layoutId(): Int
public interface Callback {
fun onFragmentAttached()
fun onFragmentDetached(tag: String)
}
}
飞行活动
class FlightPricesActivity : BaseActivity() {
@Inject
lateinit var flightPricesFragment: FlightPricesFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.flight_price_list)
addFragment(flightPricesFragment, R.id.flightPricesContainer)
}
}
航班碎片
class FlightPricesFragment: BaseFragment(), IFlightPricesView {
override fun layoutId() = R.layout.flight_prices_fragment
override fun showFlightPrices(flightPrices: FlightPricesResults) {
// connect to recycleview
}
override fun loadingStarted() {
}
override fun loadingFailed(errorMessage: String?) {
}
}
活动模块
@Module
internal class FlightPricesActivityModule {
@Provides
fun provideFlightPricesPresenter(flightPricesApi: FlightPricesApi) : IFlightPricesPresenter {
return FlightPricesPresenter(flightPricesApi)
}
@Provides
fun provideFlightPricesApi(retrofit: Retrofit) : FlightPricesApi {
return retrofit.create(FlightPricesApi::class.java)
}
@Provides
fun provideFlightPricesRVAdapter() : FlightPricesRVAdapter {
return FlightPricesRVAdapter()
}
}
片段模块提供程序
@Module
public abstract class FlightPricesFragmentProvider {
@ContributesAndroidInjector(modules = [FlightPricesFragmentModule::class])
abstract fun provideFlightPricesFragment(): FlightPricesFragment
}
片段模块
@Module
class FlightPricesFragmentModule {
@Provides
@FragmentScope
fun provideFlightPricesFragment() : FlightPricesFragment {
return FlightPricesFragment()
}
}
活动生成器
@Module
public abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [FlightPricesActivityModule::class, FlightPricesFragmentProvider::class])
public abstract fun bindFlightPricesActivity(): FlightPricesActivity
}
Dagger 2.21依赖
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
kapt "com.google.dagger:dagger-compiler:2.21"
kapt "com.google.dagger:dagger-android-processor:2.21"
这是堆栈跟踪错误(已更新):
/app/build/tmp/kapt3/stubs/debug/com/reza/skyscannertest/di/component/AppComponent.java:8: error: [Dagger/MissingBinding] com.reza.skyscannertest.ui.flightPrices.view.FlightPricesFragment cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.reza.skyscannertest.BaseApp> {
^
A binding with matching key exists in component: com.reza.skyscannertest.di.module.flightPrices.FlightPricesFragmentProvider_ProvideFlightPricesFragment.FlightPricesFragmentSubcomponent
com.reza.skyscannertest.ui.flightPrices.view.FlightPricesFragment is injected at
com.reza.skyscannertest.ui.flightPrices.view.FlightPricesActivity.flightPricesFragment
com.reza.skyscannertest.ui.flightPrices.view.FlightPricesActivity is injected at
dagger.android.AndroidInjector.inject(T) [com.reza.skyscannertest.di.component.AppComponent → com.reza.skyscannertest.di.module.ActivityBuilder_BindFlightPricesActivity.FlightPricesActivitySubcomponent]
我尝试提供flightFragment但看起来好像不去那里。
答案 0 :(得分:0)
我应该为片段添加构造函数作为匕首,以寻找构造函数以构建提供程序
class FlightPricesPresenter @Inject constructor(flightPricesApi: FlightPricesApi) : IFlightPricesPresenter {
感谢我三天的阅读文档,但是值得一提的是用匕首2.21代替了匕首2.10:)