我正在尝试实现一个允许浏览电影并检查它们的评分的电影应用程序......当我尝试使用我的 MovieListViewModel 实现 MVVM 模型时遇到问题,其中调试器告诉我我无法创建实例这个类的(MovieListViewModel)。
我所做的是我尝试遵循一些教程,我在这个站点上查找了关于视图模型的所有问题,他们通常会说我应该将我的视图模型的构造函数作为公共构造函数,或者构造函数中存在其他问题(参数和类似的东西)但我确信我遵循了关于构造函数的指南
(我尝试按照本教程来实现我的应用程序的第一部分) https://medium.com/@eladb4382/paging-library-viewmodel-livedata-room-and-retrofit-66bf6a0eef9d 我做了基本相同的代码,但我有他没有的错误...
这是我的列表视图模型类:
公共类 MovieListViewModel 扩展 AndroidViewModel {
private MovieRepository repository;
public MovieListViewModel(@NonNull Application application) {
super(application);
repository = MovieRepository.getInstance(application);
}
public LiveData<PagedList<Movie>> getMovies() {
return repository.getMovies();
}
public LiveData<NetworkState> getNetworkState() {
return repository.getNetworkState();
}}
问题出在哪里:
公共类 MovieListFragment 扩展 Fragment 实现 OnMovieItemClicked {
private final String TAG = MovieListFragment.class.getSimpleName() ;
protected MovieListViewModel viewmodel ;
private MovieDetailsViewModel movieDetailsViewModel ;
protected RecyclerView recyclerView ;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.movie_list_fragment , container , false );
recyclerView = view.findViewById(R.id.moviesRecyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
Log.d(TAG , "creating viewmodel ..." );
viewmodel = ViewModelProviders.of(getActivity()).get(MovieListViewModel.class);
observersRegisters() ;
return view;
}
private void observersRegisters() {
final MovieAdapter adapter = new MovieAdapter(this);
viewmodel.getMovies().observe(this , adapter ::submitList);
viewmodel.getNetworkState().observe(this,networkState ->{
adapter.setNetworkState(networkState);
});
recyclerView.setAdapter(adapter);
movieDetailsViewModel = ViewModelProviders.of(getActivity()).get(MovieDetailsViewModel.class);
}
(这不是整个片段类,它只是发生错误的部分)
我称之为这个片段的活动在这里::
公共类 MainActivity 扩展 AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName() ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragmentsContainer)!= null) {
if(savedInstanceState != null ){
return ;
}
MovieListFragment listFragment = new MovieListFragment() ;
listFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragmentsContainer,listFragment).commit();
}}
我还在 GitHub 上上传了整个项目,以防有人想检查问题:
https://github.com/TheDeathLorD/MovieApp
这是我在运行时遇到的错误:
E/AndroidRuntime:致命异常:主进程:com.example.movieapp2,PID:4935 java.lang.RuntimeException:无法在 android.arch.lifecycle.ViewModelProvider 创建类 com.example.movieapp2.ui.viewmodel.MovieListViewModel 的实例$AndroidViewModelFactory.create(ViewModelProvider.java:207) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) at com.example。 movieapp2.ui.view.MovieListFragment.onCreateView(MovieListFragment.java:40)