英文:
Provides methods must return a value (not void)
问题
以下是翻译好的内容:
当我尝试使用Dagger 2时,出现了以下错误,我应该怎么做?我使用的是Dagger 2新版。
e:/Users/stepanbezhuk/Documents/Work/Rebus/Movie/app/build/tmp/kapt3/stubs/debug/com/popularmovies/movies/Service/MessageWorking.java:11: 错误:@Provides方法必须返回一个值(而不是void)
public final void messageConnected$app_debug(boolean networkStatus) {
模块1
@Module
open class MessageWorking @Inject constructor(private val context: Context) {
@Provides
internal fun messageConnected(networkStatus: Boolean): String {
if (networkStatus) {
showMessage(message = "连接")
} else {
showMessage(message = "断开连接")
}
}
private fun showMessage(message: String) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
}
接口
@Component(modules = arrayOf(MessageWorking::class))
@Singleton
interface DaggerComponent {
fun messageConnected(networkStatus: Boolean)
}
英文:
When I try to use Dagger 2 it gives this error, what should I do, I'm in Dagger 2 new
e: /Users/stepanbezhuk/Documents/Work/Rebus/Movie/app/build/tmp/kapt3/stubs/debug/com/popularmovies/movies/Service/MessageWorking.java:11: error: @Provides methods must return a value (not void)
public final void messageConnected$app_debug(boolean networkStatus) {
Module 1
@Module
open class MessageWorking @Inject constructor(private val context: Context) {
@Provides
internal fun messageConnected(networkStatus: Boolean) {
if (networkStatus) {
showMessage(message = "Connect")
} else {
showMessage(message = "Disconnect")
}
}
private fun showMessage(message: String) {
return Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
}
Interface
@Component(modules = arrayOf(MessageWorking::class))
@Singleton
interface DaggerComponent {
fun messageConnected(networkStatus: Boolean)
}
答案1
得分: 2
@Provides
注解表示您必须提供返回内容。在messageConnected()
中,您没有返回类型。
您正在Dagger内部执行UI操作(toast),这非常奇怪,因为Dagger的责任是提供依赖关系。
这是一个关于Dagger的简单介绍以及设置方法的链接:
https://medium.com/@skshayne/understanding-dagger-as-a-beginner-8ffe29bad429
英文:
The @Provides
annotations mean you must provide something back. you have no return type in messageConnected()
.
You are doing UI stuff inside Dagger (toast), which is very odd since the dagger's responsibility is to provide dependencies.
Here is a link to a simple intro to Dagger and how to setup:
https://medium.com/@skshayne/understanding-dagger-as-a-beginner-8ffe29bad429
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论