如果没有 @提供的通知方法,就无法提供匕首刀

发布于 2025-01-31 18:00:21 字数 4211 浏览 3 评论 0原文

我是新来的匕首。因此,我无法解决这个问题。我只想在这里要求解决它。

这是错误:

c:\ users \ msi \ documents \ myandroidprojects \ moverprojects \ app \ build \ build \ build \ hilt \ hilt \ component_sources \ debug \ debug \ com \ com \ example \ example \ appieapp \ app_hiltcomponents.java:128:128:128:128:128:128:128:128:128:128:128:128:128: 错误:[匕首/丢失订阅] com.example.movi​​eapp.api.movi​​eappservice,没有一个 @提供通知的方法。公共摘要静态类Singletonc 实现app_generatedInjector, ^ com.example.movi​​eapp.api.movi​​eappservice被注入 com.example.movi​​eapp.repository.movi​​erepository(movieAppService) com.example.movi​​eapp.repository.movi​​erepository被注入 com.example.movi​​eapp.viewmodel.mainviewmodel(存储库,�) com.example.movi​​eapp.viewmodel.mainviewmodel被注入 com.example.movi​​eapp.viewmodel.mainviewmodel_hiltmodules.bindsmodule.binds(arg0) @dagger.hilt.android.internal.lifecycle.hiltviewmodelmap java.util.map< java.lang.string,javax.inject.inject.provider< androidx.lifecycle.viewmodcle.viewmodel> 请求 Dagger.hilt.android.internal.lifecycle.hiltviewmodelfactory.viewmodelfactoriesentrypoint.gethiltViewModelMap() [com.example.movi​​eapp.app_hiltcomponents.singletonc? com.example.movi​​eapp.app_hiltcomponents.ActivityReTivityC? com.example.movi​​eapp.app_hiltcomponents.viewmodelc]

mainviewModel.kt

@HiltViewModel
class MainViewModel@Inject constructor(
    private val repository: MovieRepository,
    @ApplicationContext private val context: Context
) : ViewModel() {

    val movieList = MutableLiveData<Resource<Movie>>()

    fun getAllMovies(movieName: String) {
        movieList.postValue(Resource.Loading())
        viewModelScope.launch {
            try {
                if (hasInternetConnection(context)) {
                    val response = repository.getMovies(movieName, "ffe9063f")
                    movieList.postValue(Resource.Success(response.body()!!))
                } else
                    movieList.postValue(Resource.Error("Internet yok"))
            } catch (ex: Exception) {
                when (ex) {
                    is IOException -> movieList.postValue(Resource.Error("Network Failure " + ex.localizedMessage))
                    else -> movieList.postValue(Resource.Error("Conversion Error"))
                }
            }
        }
    }
}

movierepository.kt

@Singleton
class MovieRepository @Inject constructor(private val movieAppService: MovieAppService) {

    suspend fun getMovies(title: String, aKey: String): Response<Movie> = withContext(
        Dispatchers.IO
    ) {
        val movies = movieAppService.getMovies(title = title, aKey = aKey)
        movies
    }
}

apimodule.kt

class ApiModule {
    @Module
    @InstallIn(SingletonComponent::class)
    object ApiModule {

        @Provides
        @Singleton
        fun provideLoggingInterceptor(): HttpLoggingInterceptor {
            return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
        }

        @Provides
        @Singleton
        fun provideOkHttpClient(logging: HttpLoggingInterceptor): OkHttpClient {
            return OkHttpClient.Builder()
                .addInterceptor(logging)
                .connectTimeout(15, TimeUnit.SECONDS) // connect timeout
                .readTimeout(15, TimeUnit.SECONDS)
                .build()
        }

        @Provides
        @Singleton
        fun provideRetrofit(client: OkHttpClient): Retrofit {
            return Retrofit.Builder()
                .baseUrl(ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build()
        }

        @Provides
        @Singleton
        fun provideMovieAppService(retrofit: Retrofit): MovieAppService {
            return retrofit.create(MovieAppService::class.java)
        }
    }
}

movieAppservice.kt

interface MovieAppService {

    companion object {
        const val ENDPOINT = "http://www.omdbapi.com/"
    }

    @GET(".")
    suspend fun getMovies(@Query("t") title: String,@Query("apikey") aKey: String): Response<Movie>
}

I am new with dagger using. So, I can't solve whats the problem with this. I just want to ask here to solve it.

This is the error:

C:\Users\msi\Documents\MyAndroidProjects\MovieProjects\app\build\generated\hilt\component_sources\debug\com\example\movieapp\App_HiltComponents.java:128:
error: [Dagger/MissingBinding]
com.example.movieapp.api.MovieAppService cannot be provided without an
@Provides-annotated method. public abstract static class SingletonC
implements App_GeneratedInjector,
^
com.example.movieapp.api.MovieAppService is injected at
com.example.movieapp.repository.MovieRepository(movieAppService)
com.example.movieapp.repository.MovieRepository is injected at
com.example.movieapp.viewmodel.MainViewModel(repository, �)
com.example.movieapp.viewmodel.MainViewModel is injected at
com.example.movieapp.viewmodel.MainViewModel_HiltModules.BindsModule.binds(arg0)
@dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>>
is requested at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap()
[com.example.movieapp.App_HiltComponents.SingletonC ?
com.example.movieapp.App_HiltComponents.ActivityRetainedC ?
com.example.movieapp.App_HiltComponents.ViewModelC]

MainViewModel.kt

@HiltViewModel
class MainViewModel@Inject constructor(
    private val repository: MovieRepository,
    @ApplicationContext private val context: Context
) : ViewModel() {

    val movieList = MutableLiveData<Resource<Movie>>()

    fun getAllMovies(movieName: String) {
        movieList.postValue(Resource.Loading())
        viewModelScope.launch {
            try {
                if (hasInternetConnection(context)) {
                    val response = repository.getMovies(movieName, "ffe9063f")
                    movieList.postValue(Resource.Success(response.body()!!))
                } else
                    movieList.postValue(Resource.Error("Internet yok"))
            } catch (ex: Exception) {
                when (ex) {
                    is IOException -> movieList.postValue(Resource.Error("Network Failure " + ex.localizedMessage))
                    else -> movieList.postValue(Resource.Error("Conversion Error"))
                }
            }
        }
    }
}

MovieRepository.kt

@Singleton
class MovieRepository @Inject constructor(private val movieAppService: MovieAppService) {

    suspend fun getMovies(title: String, aKey: String): Response<Movie> = withContext(
        Dispatchers.IO
    ) {
        val movies = movieAppService.getMovies(title = title, aKey = aKey)
        movies
    }
}

ApiModule.kt

class ApiModule {
    @Module
    @InstallIn(SingletonComponent::class)
    object ApiModule {

        @Provides
        @Singleton
        fun provideLoggingInterceptor(): HttpLoggingInterceptor {
            return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
        }

        @Provides
        @Singleton
        fun provideOkHttpClient(logging: HttpLoggingInterceptor): OkHttpClient {
            return OkHttpClient.Builder()
                .addInterceptor(logging)
                .connectTimeout(15, TimeUnit.SECONDS) // connect timeout
                .readTimeout(15, TimeUnit.SECONDS)
                .build()
        }

        @Provides
        @Singleton
        fun provideRetrofit(client: OkHttpClient): Retrofit {
            return Retrofit.Builder()
                .baseUrl(ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build()
        }

        @Provides
        @Singleton
        fun provideMovieAppService(retrofit: Retrofit): MovieAppService {
            return retrofit.create(MovieAppService::class.java)
        }
    }
}

MovieAppService.kt

interface MovieAppService {

    companion object {
        const val ENDPOINT = "http://www.omdbapi.com/"
    }

    @GET(".")
    suspend fun getMovies(@Query("t") title: String,@Query("apikey") aKey: String): Response<Movie>
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-07 18:00:21

不要用相同的名称类包装单身对象模块。更改您的模块文件或更改您的类名称

@Module
@InstallIn(SingletonComponent::class)
object ApiModule {
    
    @Provides
    @Singleton
    fun provideLoggingInterceptor(): HttpLoggingInterceptor {
        return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
    }

    @Provides
    @Singleton
    fun provideOkHttpClient(logging: HttpLoggingInterceptor): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(logging)
            .connectTimeout(15, TimeUnit.SECONDS) // connect timeout
            .readTimeout(15, TimeUnit.SECONDS)
            .build()
    }

    @Provides
    @Singleton
    fun provideRetrofit(client: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(ENDPOINT)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build()
    }

    @Provides
    @Singleton
    fun provideMovieAppService(retrofit: Retrofit): MovieAppService {
        return retrofit.create(MovieAppService::class.java)
    }
    
}

Do not wrap your singleton object module with a same named class. Change your module file like this or change your class name

@Module
@InstallIn(SingletonComponent::class)
object ApiModule {
    
    @Provides
    @Singleton
    fun provideLoggingInterceptor(): HttpLoggingInterceptor {
        return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
    }

    @Provides
    @Singleton
    fun provideOkHttpClient(logging: HttpLoggingInterceptor): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor(logging)
            .connectTimeout(15, TimeUnit.SECONDS) // connect timeout
            .readTimeout(15, TimeUnit.SECONDS)
            .build()
    }

    @Provides
    @Singleton
    fun provideRetrofit(client: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(ENDPOINT)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build()
    }

    @Provides
    @Singleton
    fun provideMovieAppService(retrofit: Retrofit): MovieAppService {
        return retrofit.create(MovieAppService::class.java)
    }
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文