You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The BaseActivity I use encapsulates BaseViewModel, and an error is reported during compilation. The code and error message are as follows
error message:
public abstract static class SingletonC implements AppApplication_GeneratedInjector,
^
Injection of an @HiltViewModel class is prohibited since it does not create a ViewModel instance correctly.
Access the ViewModel via the Android APIs (e.g. ViewModelProvider) instead.
Injected ViewModel: com.maxvision.myapplication.YourViewModel
com.maxvision.myapplication.YourViewModel is injected at
com.maxvision.myapplication.base.BaseActivity.viewModel
com.maxvision.myapplication.MainActivity is injected at
com.maxvision.myapplication.MainActivity_GeneratedInjector.injectMainActivity(com.maxvision.myapplication.MainActivity) [com.maxvision.myapplication.AppApplication_HiltComponents.SingletonC → com.maxvision.myapplication.AppApplication_HiltComponents.ActivityRetainedC → com.maxvision.myapplication.AppApplication_HiltComponents.ActivityC]
code show as below:
`abstract class BaseActivity <VM : BaseViewModel, DB : ViewDataBinding> : AppCompatActivity() {
@Inject
lateinit var viewModel: VM
lateinit var binding: DB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, getLayoutResId())
binding.lifecycleOwner = this
}
//获取布局资源id
abstract fun getLayoutResId(): Int
}`
`
@HiltViewModel
class YourViewModel @Inject constructor(
private val apiService: ApiService
) : BaseViewModel() {
}
`
` @androidentrypoint
class MainActivity : BaseActivity<YourViewModel, ActivityMainBinding>(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun getLayoutResId(): Int = R.layout.activity_main
}
@HiltAndroidApp
class AppApplication : Application() {
override fun onCreate() {
super.onCreate()
}
companion object {
private val sInstance: Application? = null
}
}
`
The text was updated successfully, but these errors were encountered:
The BaseActivity I use encapsulates BaseViewModel, and an error is reported during compilation. The code and error message are as follows
error message:
public abstract static class SingletonC implements AppApplication_GeneratedInjector,
^
Injection of an @HiltViewModel class is prohibited since it does not create a ViewModel instance correctly.
Access the ViewModel via the Android APIs (e.g. ViewModelProvider) instead.
Injected ViewModel: com.maxvision.myapplication.YourViewModel
code show as below:
`abstract class BaseActivity <VM : BaseViewModel, DB : ViewDataBinding> : AppCompatActivity() {
}`
`
@HiltViewModel
class YourViewModel @Inject constructor(
private val apiService: ApiService
) : BaseViewModel() {
}
`
`
@androidentrypoint
class MainActivity : BaseActivity<YourViewModel, ActivityMainBinding>(){
}
@HiltAndroidApp
class AppApplication : Application() {
}
`
The text was updated successfully, but these errors were encountered: