Android视图模型类没有零参数构造函数,在片段中引发实例化异常。

huangapple go评论112阅读模式
英文:

Android view model class has no zero argument constructor,Instantiation Exception in fragment

问题

我有一个视图模型类,需要在片段中实例化它。但是我遇到了以下问题:

  1. java.lang.RuntimeException: 无法创建 com.example.project.favourites.FavViewModel 类的实例

以及

  1. Caused by: java.lang.InstantiationException: java.lang.Class<com.example.project.favourites.FavViewModel> 没有零参数构造函数

导致崩溃的代码行是:

  1. favViewModel = new ViewModelProvider(this).get(FavViewModel.class);
  2. (此行位于片段的 onViewCreated 方法中)

请帮忙!!!

以下是 FavViewModel 类的内容:

  1. public class FavViewModel extends AndroidViewModel {
  2. private FavRepository repository;
  3. private LiveData<List<FavItem>> allFav;
  4. public FavViewModel(@NonNull Application application) {
  5. super(application);
  6. repository = new FavRepository(application);
  7. allFav = repository.getAllFav();
  8. }
  9. public void insert(FavItem favItem) {
  10. repository.insert(favItem);
  11. }
  12. public void delete(FavItem favItem) {
  13. repository.delete(favItem);
  14. }
  15. public void deleteAll() {
  16. repository.deleteAll();
  17. }
  18. public LiveData<List<FavItem>> getAllFav() {
  19. return allFav;
  20. }
  21. }
英文:

i have a view model class and i need to instantiate it in a fragment.
But I am getting :

  1. java.lang.RuntimeException: Cannot create an instance of class com.example.project.favourites.FavViewModel

and

  1. Caused by: java.lang.InstantiationException: java.lang.Class<com.example.project.favourites.FavViewModel> has no zero argument constructor

This is the line causing crash:

  1. favViewModel= new ViewModelProvider(this).get(FavViewModel.class);
  2. (This line is within onViewCreated in fragment)

pls help!!!!!

Below is FavViewModel Class

  1. public class FavViewModel extends AndroidViewModel {
  2. private FavRepository repository;
  3. private LiveData<List<FavItem>> allFav;
  4. public FavViewModel(@NonNull Application application) {
  5. super(application);
  6. repository=new FavRepository(application);
  7. allFav=repository.getAllFav();
  8. }
  9. public void insert(FavItem favItem){
  10. repository.insert(favItem);
  11. }
  12. public void delete(FavItem favItem){
  13. repository.delete(favItem);
  14. }
  15. public void deleteAll(){
  16. repository.deleteAll();
  17. }
  18. public LiveData<List<FavItem>> getAllFav(){
  19. return allFav;
  20. }
  21. }
  22. </details>
  23. # 答案1
  24. **得分**: 1
  25. 你可以使用以下注解为你的模型类进行注解:
  26. **`@NoArgsConstructor`**
  27. 你可以通过此链接[**`lombok`**][1]深入了解lombokNoArgsConstructor和其他许多注解。
  28. [1]: https://projectlombok.org/features/constructor
  29. <details>
  30. <summary>英文:</summary>
  31. You can annotate your model class with below annotation
  32. **`@NoArgsConstructor`**
  33. You will get more idea about lombok , NoArgsConstructor and many more annotations using this link[**`lombok`**][1]
  34. [1]: https://projectlombok.org/features/constructor
  35. </details>
  36. # 答案2
  37. **得分**: 0
  38. 尝试使用这个...
  39. 像这样初始化 `ViewModel`。你还需要在 `ViewModelProvider` 构造函数中传递 `ViewModelFactory`
  40. ```java
  41. favViewModel = new ViewModelProvider(this,
  42. new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(FavViewModel.class);
  43. ```
  44. 希望对你有帮助。如有疑问,请随意提问...
  45. <details>
  46. <summary>英文:</summary>
  47. Try using this...
  48. Initialise `ViewModel` like this. You need to also pass `ViewModelFactory` with `ViewModelProvider` constructor.
  49. ```
  50. favViewModel = new ViewModelProvider(this,
  51. new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(FavViewModel.class);
  52. ```
  53. Hope this helps. Feel free to ask for clarifications...
  54. </details>

huangapple
  • 本文由 发表于 2020年8月23日 22:12:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63547942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定