error: [Dagger/MissingBinding] java.lang.Integer不能在没有@Inject



我正在开发一个Retrofit 2 kotlin应用程序,一切都很好,直到我尝试实现另一个功能,让用户通过id过滤到不同用户的API。

应用程序的第一种工作方式是调用所有的用户,在我的ViewModel的onClick监听器中,我恢复其中一个,并显示在屏幕上。

所以,我尝试做同样的事情,但我添加了一个EditText来让用户按id过滤,当我实现这个函数时,我开始得到以下错误:

error: [Dagger/MissingBinding] java.lang.Integer cannot be provided without an @Inject constructor or an @Provides-annotated method.

所以,我的kotlin类如下:

MainActivity.kt

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var Etid: EditText
private lateinit var id: Number
private val quoteViewModel: QuoteViewModel by viewModels()
private var userSearch: QuoteModel = TODO()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Pasa el layout a linkear con el binding.
binding = ActivityMainBinding.inflate(layoutInflater)
//settemos la vista
setContentView(binding.root)
//Inicilizamos el controlador UI-data (ViewModel)
quoteViewModel.onCreate()
//Inicilizamos el EditText.
Etid.findViewById(R.id.EtId) as EditText
//Establecemos un observer en los datos
quoteViewModel.quoteModel.observe(this, Observer {
binding.tvName.text = it.name
binding.tvEmail.text = it.email
binding.tvAddress.text = it.addrees
binding.tvPhone.text = it.phone
binding.tvWebsite.text = it.website
binding.tvCompany.text = it.company
})
//Establecemos un observer en la carga de la página.
quoteViewModel.isLoading.observe(this, Observer {
binding.loading.isVisible = it
})


binding.viewContainer.setOnClickListener {
id = Etid.id;
userSearch = quoteViewModel.getUserIdQuote(id as Int) as QuoteModel
it.findViewById<TextView>(R.id.tvName).text = userSearch.name
it.findViewById<TextView>(R.id.tvAddress).text  = userSearch.addrees
it.findViewById<TextView>(R.id.tvCompany).text = userSearch.company
it.findViewById<TextView>(R.id.tvEmail).text = userSearch.email
it.findViewById<TextView>(R.id.tvPhone).text = userSearch.phone
it.findViewById<TextView>(R.id.tvWebsite).text = userSearch.website
}
}
}

关于Retrofit配置:

NetworkModule.kt


@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit():Retrofit{
return Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Singleton
@Provides
fun provideQuoteApiClient(retrofit: Retrofit):QuoteApiClient{
return retrofit.create(QuoteApiClient::class.java)
}
}

我的数据模型如下:

data class QuoteModel(
@SerializedName("id") val quote: Number,
@SerializedName("name") val name: String,
@SerializedName("email") val email: String,
@SerializedName("address") val addrees: String,
@SerializedName("phone") val phone: String,
@SerializedName("website") val website: String,
@SerializedName("company") val company: String,
)`

我的提供商服务和存储库是:

QuoteProvider.kt

@Singleton
class QuoteProvider @Inject constructor() {
var allUsers: List<QuoteModel> = emptyList1()
var userById: QuoteModel = listOf<QuoteModel>() as QuoteModel
}
`

QuoteService.kt

class QuoteService @Inject constructor(private val api:QuoteApiClient) {
suspend fun getAllUsers(): List<QuoteModel> {
return withContext(Dispatchers.IO) {
val response = api.getAllUUsers()
response.body() ?: emptyList1()
}
}
suspend fun getUserById(id:Number): QuoteModel{
return withContext(Dispatchers.IO){
val response = api.getUserById(id)
response.body() ?: listOf<QuoteModel>()
} as QuoteModel
}
}

QuoteRepository.kt

class QuoteRepository @Inject constructor(
private val api: QuoteService,
private val quoteProvider: QuoteProvider
) {
suspend fun getAllUsers(): List<QuoteModel> {
//extraemos los users de la api
val response = api.getAllUsers()
//Le pasamos el [] de users al repository de la app.
quoteProvider.allUsers = response
return response
}
suspend fun getUserById(id: Number): QuoteModel {
//Recogemos el usuario pasandole el Id al QueryRepository
val response = api.getUserById(id)
quoteProvider.userById = response
return response
}
}

最后,我用两个类控制了对存储库的调用,我不知道这是否被用来这样做。

class GetUserById @Inject constructor(private val repository: QuoteRepository, private var id: Int) {
//TODO() Si no funciona probar a sacarlo del repository.
suspend  operator fun invoke(id: kotlin.Int): QuoteModel {
//val id = id
return repository.getUserById(id)
}

}

class GetAllUsers @Inject constructor(private val repository: QuoteRepository) {
suspend operator fun invoke() = repository.getAllUsers()
}

Maybe错误可能在实现的结构中,而不是在某些类上的错误代码

我希望你有一些使用改造的经验,可以帮助。

如果是这样,请提前致谢!

我认为你必须提供quoteserviceclass

@Singleton
class QuoteService @Inject constructor(private val api:QuoteApiClient) {
suspend fun getAllUsers(): List<QuoteModel> {
return withContext(Dispatchers.IO) {
val response = api.getAllUUsers()
response.body() ?: emptyList1()
}
}
suspend fun getUserById(id:Number): QuoteModel{
return withContext(Dispatchers.IO){
val response = api.getUserById(id)
response.body() ?: listOf<QuoteModel>()
} as QuoteModel
}
}

编辑:对不起,我的意思是添加单例注释而不提供

最新更新