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

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

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

问题

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

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

以及

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

导致崩溃的代码行是:

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

请帮忙!!!

以下是 FavViewModel 类的内容:

public class FavViewModel extends AndroidViewModel {
    private FavRepository repository;
    private LiveData<List<FavItem>> allFav;

    public FavViewModel(@NonNull Application application) {
        super(application);
        repository = new FavRepository(application);
        allFav = repository.getAllFav();
    }

    public void insert(FavItem favItem) {
        repository.insert(favItem);
    }

    public void delete(FavItem favItem) {
        repository.delete(favItem);
    }

    public void deleteAll() {
        repository.deleteAll();
    }

    public LiveData<List<FavItem>> getAllFav() {
        return allFav;
    }
}
英文:

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

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

and

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

This is the line causing crash:

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

pls help!!!!!

Below is FavViewModel Class

public class FavViewModel extends AndroidViewModel {
    private FavRepository repository;
    private LiveData<List<FavItem>> allFav;

    public FavViewModel(@NonNull Application application) {
        super(application);
        repository=new FavRepository(application);
        allFav=repository.getAllFav();
    }


    public void insert(FavItem favItem){
        repository.insert(favItem);
    }

    public void delete(FavItem favItem){
        repository.delete(favItem);
    }

    public void deleteAll(){
        repository.deleteAll();
    }
    public LiveData<List<FavItem>> getAllFav(){
        return allFav;
    }
}


</details>


# 答案1
**得分**: 1

你可以使用以下注解为你的模型类进行注解:

**`@NoArgsConstructor`**

你可以通过此链接[**`lombok`**][1]深入了解lombok、NoArgsConstructor和其他许多注解。

  [1]: https://projectlombok.org/features/constructor

<details>
<summary>英文:</summary>

You can annotate your model class with below annotation

**`@NoArgsConstructor`**

You will get more idea about lombok , NoArgsConstructor and many more annotations using this link[**`lombok`**][1]


  [1]: https://projectlombok.org/features/constructor

</details>



# 答案2
**得分**: 0

尝试使用这个...

像这样初始化 `ViewModel`。你还需要在 `ViewModelProvider` 构造函数中传递 `ViewModelFactory`。

```java
favViewModel = new ViewModelProvider(this,
                new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(FavViewModel.class);
```

希望对你有帮助。如有疑问,请随意提问...

<details>
<summary>英文:</summary>

Try using this...

Initialise `ViewModel` like this. You need to also pass `ViewModelFactory` with `ViewModelProvider` constructor.

```
favViewModel = new ViewModelProvider(this,
                new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(FavViewModel.class);
```

Hope this helps. Feel free to ask for clarifications...

</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:

确定