英文:
Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin)
问题
在许多网站和各种文章中搜索过,但没有效果,因此我请求您帮助解决Dagger 2(DI)的问题。
我无法理解问题出在哪里。
我对Dagger 2(Di)还很新,正在学习中,请帮助我理解。
我的代码:
MainActivity.kt
class MainActivity : AppCompatActivity() {
@Inject
lateinit var networkService: NetworkService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App().gerAppComponents().inject(mainActivity = this)
setContentView(R.layout.activity_main)
networkService.buildService(service = Movies::class.java).getPopularMovie(apiKey = "test", language = "en-US", page = 1)
}
}
App.kt
class App : Application() {
fun gerAppComponents(): AppComponent {
return DaggerAppComponent.builder().appNetworkModule(AppNetworkModule())
.build()
}
}
ServiceBuild.kt
object ServiceBuild : NetworkService {
private val client = OkHttpClient.Builder().build()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.themoviedb.org/3")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
override fun <T> buildService(service: Class<T>): T = retrofit.create(service)
}
NetworkService.kt
interface NetworkService {
fun <T> buildService(service: Class<T>): T
}
AppNetworkModule.kt
@Module
class AppNetworkModule {
@Provides
@Singleton
fun getRetrofitService(): ServiceBuild {
return ServiceBuild
}
}
AppComponent.kt
@Singleton
@Component(modules = arrayOf(AppNetworkModule::class))
interface AppComponent {
fun inject(mainActivity: MainActivity)
}
构建错误:
app/build/tmp/kapt3/stubs/debug/com/rebus/client/di/components/AppComponent.java:10: error: [Dagger/MissingBinding] com.rebus.client.services.impl.NetworkService cannot be provided without an @Provides-annotated method.
public abstract void inject(@org.jetbrains.annotations.NotNull()
^
com.rebus.client.services.impl.NetworkService is injected at
com.rebus.client.MainActivity.networkService
com.rebus.client.MainActivity is injected at
com.rebus.client.di.components.AppComponent.inject(com.rebus.client.MainActivity)
英文:
searched on many sites and in various articles, but without effect, so I ask for your help with Dagger 2 (DI)
I can not understand what the problem is
I'm new to Dagger 2 (Di) and I'm just studying it, please help me understand
My code:
MainActivity.kt
class MainActivity : AppCompatActivity() {
@Inject
lateinit var networkService: NetworkService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App().gerAppComponents().inject(mainActivity = this)
setContentView(R.layout.activity_main)
networkService.buildService(service = Movies::class.java).getPopularMovie(apiKey = "test", language = "en-US", page = 1)
}
}
App.kt
class App : Application() {
fun gerAppComponents(): AppComponent {
return DaggerAppComponent.builder().appNetworkModule(AppNetworkModule())
.build()
}
}
ServiceBuild.kt
object ServiceBuild : NetworkService {
private val client = OkHttpClient.Builder().build()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.themoviedb.org/3")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
override fun <T> buildService(service: Class<T>): T = retrofit.create(service)
}
NetworkService.kt
interface NetworkService {
fun <T> buildService(service: Class<T>): T
}
AppNetworkModule.kt
@Module
class AppNetworkModule {
@Provides
@Singleton
fun getRetrofitService(): ServiceBuild {
return ServiceBuild
}
}
AppComponent.kt
@Singleton
@Component(modules = arrayOf(AppNetworkModule::class))
interface AppComponent {
fun inject(mainActivity: MainActivity)
}
Build error
app/build/tmp/kapt3/stubs/debug/com/rebus/client/di/components/AppComponent.java:10: error: [Dagger/MissingBinding] com.rebus.client.services.impl.NetworkService cannot be provided without an @Provides-annotated method.
public abstract void inject(@org.jetbrains.annotations.NotNull()
^
com.rebus.client.services.impl.NetworkService is injected at
com.rebus.client.MainActivity.networkService
com.rebus.client.MainActivity is injected at
com.rebus.client.di.components.AppComponent.inject(com.rebus.client.MainActivity)
答案1
得分: 0
将你的代码中的 ServiceBuild
改成 NetworkService
,就像这样:
@Module
class AppNetworkModule {
@Provides
@Singleton
fun getRetrofitService(): NetworkService {
return NetworkService
}
}
英文:
Change your code ServiceBuild
to NetworkService
like this:
@Module
class AppNetworkModule {
@Provides
@Singleton
fun getRetrofitService(): NetworkService {
return ServiceBuild
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论