android - 无法创建 ViewModel 类的实例(无法启动 Activity ComponentInfo)

标签 android mvvm android-architecture-components android-viewmodel

我在我的项目中使用 MVVM、Retrofit、LiveData,但在我看到这些链接之前出现此错误

错误

java.lang.RuntimeException:       
Unable to start activity ComponentInfo{ir.orangehat.movieinfo/ir.orangehat.movieinfo.application.home.HomeActivity}: java.lang.RuntimeException:

Cannot create an instance of class ir.orangehat.movieinfo.application.home.HomeViewModel
                  

View 模型

我认为问题出在我的构造函数中

public class HomeViewModel extends AndroidViewModel {

private MovieRepository movieRepository;

public HomeViewModel(@NonNull Application application, Context context, LifecycleOwner lifecycleOwner) {
    super(application);
    movieRepository = new MovieRepository(lifecycleOwner, context);
}

LiveData<List<Movie>> getMovies() {
    return movieRepository.getMovies();
}}

存储库

public class MovieRepository extends BaseRepository {

private LifecycleOwner lifecycleOwner;
private MovieApi movieApi;
private MovieDatabaseHelper movieDatabaseHelper;

public MovieRepository(LifecycleOwner lifecycleOwner, Context context) {
    this.lifecycleOwner = lifecycleOwner;
    movieApi = getRetrofitHelper().getService(MovieApi.class);
    movieDatabaseHelper = new MovieDatabaseHelper(context);
}

public LiveData<List<Movie>> getMovies() {
    LiveData<List<Movie>> moviesLiveData = movieApi.getMovieList();
    moviesLiveData.observe(lifecycleOwner, new Observer<List<Movie>>() {
        @Override
        public void onChanged(@Nullable List<Movie> movieArrayList) {
            movieDatabaseHelper.Save(movieArrayList);
        }
    });

    return movieDatabaseHelper.getAll();
} }

Activity 类

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // the error is here
        HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);

        homeViewModel.getMovies().observe(HomeActivity.this, new Observer<List<Movie>>() {
            @Override
            public void onChanged(@Nullable List<Movie> movieArrayList) {
                String str = null;
                if (movieArrayList != null) {
                    str = Arrays.toString(movieArrayList.toArray());
                }
                Log.e("movies", str);
            }
        });
    }
}

我应该在我的项目和自定义工厂中使用 Dagger 吗?

最佳答案

引用 the documentation for AndroidViewModel :

Subclasses must have a constructor which accepts Application as the only parameter.

您的构造函数不符合该要求。

或者:

  • HomeViewModel 中删除 Context 上下文LifecycleOwner lifecycleOwner 构造函数参数,或者

  • 创建 a ViewModelProvider.Factory可以构建您的 HomeViewModel 实例,并将该工厂与 ViewModelProviders.of()

  • 一起使用

关于android - 无法创建 ViewModel 类的实例(无法启动 Activity ComponentInfo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605672/

相关文章:

android - 对单个实时数据发出的值的序列进行单元测试

android - 禁用webview中的滚动?

mvvm - Catel 与 Xamarin.Forms

xaml - 绑定(bind)到 Xaml

c# - 应用程序设置绑定(bind)最佳实践

android - 非法状态异常 : Activity does not have a NavController set on

java - ArrayList<HashMap<String, String>> 无法正确转换为 API9 的 JSONArray

android - JSONException : java. lang.Integer 无法转换为 JSONObject`

Android:GCM 推送通知实现,在 onMessageReceived 方法中 soemtimes "from"为空

android-architecture-components - 如何使用 Jetpack 导航库在两个 Activity 之间导航