java - 提供方法必须返回一个值(非 void)
问题描述
当我尝试使用 Dagger 2 时出现此错误,我该怎么办,我在 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) {
模块 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()
}
}
界面
@Component(modules = arrayOf(MessageWorking::class))
@Singleton
interface DaggerComponent {
fun messageConnected(networkStatus: Boolean)
}
解决方案
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
推荐阅读
- java - 获取 org.hibernate.HibernateException:未配置 CurrentSessionContext
- pdf - SSRS 报告在转换为 PDF 时添加额外页面
- r - 使用 ggplotly() 时包装轴标签
- automation - 在 Discord 上自动发送消息(在后台)
- python - 模块'kivy.properties'中没有名称'ObjectProperty' - Python Kivy
- reactjs - 此版本不符合 Google Play 64 位要求 React Native 应用程序
- amazon-web-services - 使用 PowerShell 检索 AWS SNS 的加密状态
- php - 如何从日期数组中分离日期和日期?
- android - 有没有办法从 android studio 中的 webView 获取元素?
- json - 从 JSON 数组中选择特定值