Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Hilt2.48.1 version, it is not compatible with BaseViewModel #983

Open
cl-6666 opened this issue Nov 27, 2023 · 0 comments
Open

Using Hilt2.48.1 version, it is not compatible with BaseViewModel #983

cl-6666 opened this issue Nov 27, 2023 · 0 comments

Comments

@cl-6666
Copy link

cl-6666 commented Nov 27, 2023

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
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant