我试着用刀柄将viewModel注入到我的基本活动中。
abstract class BaseActivity<VB: ViewBinding, VM: BaseViewModel>(val bindingFactory(LayoutInflater) -> VB) : AppCompatActivity() {
private val viewModel: VM by viewModels<>()
lateinit var binding: VB
}
但是viewModels()函数的错误是“属性委托必须有一个'getValue(Bas
我第一次尝试安卓ViewModel和Hilt DI 我从下面的链接中了解到,要在运行时使用值初始化ViewModel,我应该使用ViewModelFactory Use a ViewModelFactory //ViewModel
class ScoreViewModel(finalScore: Int) : ViewModel() {
// The final score
var score = finalScore
init {
Log.i("ScoreViewModel", "Final score is $finalScore&
错误:如何解决这个问题,即使我没有在视图模型中执行任何注入,也会得到这个枯萎的错误。
/Users/user/Documents/Personal/android-in-app-review-engine/Application/app/build/tmp/kapt3/stubs/debug/com/inappreview/code/MainActivityViewModel.java:7: error: [Hilt]
public final class MainActivityViewModel extends androidx.lifecycle.ViewModel {
错误:
error: [Dagger/MissingBinding] java.util.ArrayList<java.lang.Integer> cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_LifecycleComponentBuilderEntryPoint,
在使用Di的情况下,Android官网的编写方式如下 // import androidx.hilt.navigation.compose.hiltViewModel
@Composable
fun MyApp() {
NavHost(navController, startDestination = startRoute) {
composable("example") { backStackEntry ->
// Creates a ViewModel from the current BackStackEntry
我尝试将一个简单的viewModel交给一个可组合的,每次都得到这个错误,我不知道它意味着什么:
java.lang.IllegalStateException: Given component holder class com.example.app.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
我的等级:
implementation("a
我试图使用Hilt将上下文传递给我的数据源类,如下所示:
public class PostDataSource extends PageKeyedDataSource<Integer, Post> {
// we will start from the first page which is 1
public static final int PAGE_SIZE = 25;
// we will start from the first page which is 1
private static final int FIRST_PAGE =
我想知道如何在使用Hilt for DI时将运行时参数传递给ViewModel的构造函数?在使用Hilt之前,我有一个如下所示的ViewModel: class ItemViewModel(private val itemId: Long) : ViewModel() {
private val repo = ItemRepository(itemId)
}
class ItemViewModelFactory(private val itemId: Long) : ViewModelProvider.Factory {
@Suppress("unchecked_cast
我试图在我的项目中使用hilt,它包含了动态特性。我正面临一个错误,我无法完全理解为什么。我遇到了这样的错误:
java.lang.ClassCastException: com.social.analysis.DaggerApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl$FragmentCImpl cannot be cast to com.social.login.intro.IntroFragment_GeneratedInjector
at com.social.login.intro.Hil
我正在我的项目中使用Hilt和MVVM,我想从activityViewModel获得一个viewModel,以便在2个activites中使用它们。但我的Android Studio显示未解析的引用。
我的应用build.gradle是这样的:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
我是Android中的依赖注入的新手,我正在尝试使用Hilt将其应用到我的应用程序中。我有一个用@HiltViewModel注释的视图模型,并且我正在尝试使用@Inject注释现场注入一个存储库。但是我一直收到这个错误: public final class AlarmsViewModel extends androidx.lifecycle.ViewModel {
^
@HiltViewModel annotated class should contain exactly one @Inject annotated constructor.
[Hilt]
我试图使用hilt注入存储库类的实现(接口位于domain模块中,data模块中的实现与data模块的实现取决于domain模块),但在构建应用程序时会收到错误。这是我的装置
在domain模块中
interface RatesRepository {
fun getRates(): Single<Rates>
}
在data模块中
class RatesRepositoryImpl @Inject constructor(
private val rateDataSource: CurrencyExchangeDataSource,
private val
我刚开始写Jetpack和Hilt。但是当我注入到ViewModel中时,我遇到了一些问题。
我得到的错误是:
java.lang.RuntimeException: Cannot create an instance of class com.example.chaes.login.viewModel.SignUpViewModel
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.chaes.login.viewModel.SignUpViewModel> has no zero
Hilt指出,如果没有@Provides注释,就不能提供这个接口:
interface PlannedListRepository {
fun getAllLists(): LiveData<List<PlannedList>>
suspend fun addList(plannedList: PlannedList)
suspend fun updateList(plannedList: PlannedList)
suspend fun deleteList(plannedList: PlannedList)
}
接口的实现:
c
Cannot create an instance of class com.comp.app.winners.WinnersViewModel
Caused by: java.lang.InstantiationException: java.lang.Class<com.comp.app.winners.WinnersViewModel> has no zero argument constructor
尝试使用句柄解析片段上的视图模型时出现错误
// Proj
ext.hilt_version = '2.32-alpha'
ext.lifecycle_vers
我在android中创建了一个新项目,并添加了所有的依赖项。都是最新的。当我构建这个项目时,我会收到这些警告。应用程序中没有代码。在我添加了依赖项之后,它们就开始显示了。
我应该就这样离开吗?
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 30
buil
这是我的viewmodel类 class HomeFeedViewModel( val applicationContext: Context, val repo: repository) : ViewModel() {
lateinit private var homeView: HomeView } 这是我的主视图界面: interface HomeView {
fun OnSuccessLoadFeed(
showPostBean: ArrayList<Data_model>,
lastPage: String,
c
我想将我的ViewModel内部片段与FragmentComponent依赖项一起使用,但我认为@ViewModelInject提供了ActivityComponent或ApplicationCoinmponent内部的ViewModel,不能将我的FragmentComponent依赖项注入ViewModel class XViewModel @ViewModelInject constructor(
private val xClass: XClass // this dependency provides in FragmentComponent
) : ViewModel()