我正在创建一个项目,其中我正在使用moviedb api来获取数据。为此,我正在使用MVVM体系结构。从MovieListFragment,我想将诸如popular,top_rated之类的类别传递给MovieViewModel,以相应地过滤电影,但是我得到了空值。
这是MovieListFragment类:-
public class MovieListFragment extends Fragment {
private MovieViewModel movieViewModel;
private static final String BASE_URL = "https://api.themoviedb.org";
private static final String API_KEY = "API_KEY";
private String category;
private GridView gridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_list, container, false);
gridView = rootView.findViewById(R.id.images_grid_view);
movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
movieViewModel.setCategory(category); // from here i am passing value
movieViewModel.getMoviesRepository().observe(getActivity(), new Observer<MovieResults>() { // error line no :- 76
@Override
public void onChanged(MovieResults movieResults) {
final List<MovieResults.ResultsBean> listOfMovies = movieResults.getResults(); // error line no :- 79
MovieListAdapter mAdapter = new MovieListAdapter(getContext(), listOfMovies);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCallback.onImageSelected(position, listOfMovies);
}
});
}
});
return rootView;
}
public void setCategory(String category) {
this.category = category;
}
}
这是MovieViewModel类:-
public class MovieViewModel extends AndroidViewModel {
private String category;
private static final String API_KEY = "api_key";
private MovieRepository repository;
private MutableLiveData<MovieResults> listOfMovies;
public MovieViewModel(@NonNull Application application) {
super(application);
Log.d("MovieViewModel", category); // but here i am getting null value
listOfMovies = repository.getListOfMovies(category, API_KEY);
}
public MutableLiveData<MovieResults> getMoviesRepository() {
return listOfMovies;
}
public void setCategory(String category) {
this.category = category;
}
}
我收到这样的错误:-
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mountblue.moviesapp.entity.MovieResults.getResults()' on a null object reference
at com.mountblue.moviesapp.fragment.MovieListFragment$2.onChanged(MovieListFragment.java:79)
at com.mountblue.moviesapp.fragment.MovieListFragment$2.onChanged(MovieListFragment.java:76)
答案 0 :(得分:0)
如果在null
行中获得字符串category
的{{1}}值,则意味着movieViewModel.setCategory(category);
的字段category
为空。
尝试检查您是否在MovieListFragment
上调用了setCategory(String category)
方法。
如果不是这种情况,请详细说明错误。例如。在哪一行发生等等。
答案 1 :(得分:0)
由于Category是由setView之前的MovieViewModel的构造函数读取的,因此它始终为null。
类别作为方法参数呢?
public class MovieViewModel extends AndroidViewModel {
private static final String API_KEY = "api_key";
private MovieRepository repository;
private MutableLiveData<MovieResults> listOfMovies = new MutableLiveData<MovieResults>();
public MovieViewModel(@NonNull Application application) {
super(application);
}
public MutableLiveData<MovieResults> getMoviesRepository(category) {
loadData(category);
return listOfMovies;
}
private void loadData(category) {
// Do an asynchronous operation to fetch MovieResults.
repository.getListOfMovies(category, API_KEY);
...
// Receive asynchronous result in callback
// Post the result after getting the asynchronous result.
listOfMovies.postValue(response)
}
}
要进行更详细的实施,需要一个存储库类。
文件: https://developer.android.com/topic/libraries/architecture/viewmodel
答案 2 :(得分:0)
您必须创建viewmodel工厂,才能将数据从活动或片段传递到viewmodel。您可以在here上引用它。
答案 3 :(得分:0)
在 MovieListFragment 类中:
首先删除这一行:movieViewModel.setCategory(category);
并在此处传递“类别”:movieViewModel.getMoviesRepository(category)
像下面的代码:
movieViewModel.getMoviesRepository(category).observe(getActivity(), new Observer<MovieResults>() { // error line no :- 76
@Override
public void onChanged(MovieResults movieResults) {
final List<MovieResults.ResultsBean> listOfMovies = movieResults.getResults(); // error line no :- 79
MovieListAdapter mAdapter = new MovieListAdapter(getContext(), listOfMovies);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCallback.onImageSelected(position, listOfMovies);
}
});
}
});
在 MovieViewModel 类中:
将这一行移到 getMoviesRepository(String category) listOfMovies = repository.getListOfMovies(category, API_KEY);
然后像这样从 setCategory()
类中删除类别变量和 MovieViewModel
方法:
public class MovieViewModel extends AndroidViewModel {
private static final String API_KEY = "api_key";
private MovieRepository repository;
private MutableLiveData<MovieResults> listOfMovies;
public MovieViewModel(@NonNull Application application) {
super(application);
}
public MutableLiveData<MovieResults> getMoviesRepository(String category) {
Log.d("MovieViewModel", category); // but here i am getting null value
listOfMovies = repository.getListOfMovies(category, API_KEY);
return listOfMovies;
}
}