我对android开发非常陌生,因为我现在才刚上一门课,所以我对目前的状况感到非常困惑,因为我正在使用目前使用的引用编写代码。
我也一直在尝试使用其他来源的引用,但遗憾的是,我无法真正理解这些引用是如何工作的。
我目前的工作是我必须创建一个应用程序,该应用程序使用片段来作为电影和电视节目的目录,以下是其中一个片段的代码:
public class MovieFragment extends Fragment {
View view;
private String[] titleMovie;
private String[] descMovie;
private TypedArray posterMovie;
private String[] genreMovie;
private String[] castMovie;
private String[] duration;
private String[] directorMovie;
private MovieAdapter adapter;
private ArrayList<Movie> movies;
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.mv_fragment,container,false);
recyclerView = view.findViewById(R.id.mv_list);
adapter = new MovieAdapter(getContext(), movies);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()) {});
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prepare();
addItem();
}
private void prepare() {
titleMovie = getResources().getStringArray(R.array.name_mv);
descMovie = getResources().getStringArray(R.array.desc_mv);
posterMovie = getResources().obtainTypedArray(R.array.poster_mv);
genreMovie = getResources().getStringArray(R.array.genre_mv);
castMovie = getResources().getStringArray(R.array.cast_mv);
directorMovie = getResources().getStringArray(R.array.director_mv);
duration = getResources().getStringArray(R.array.duration);
}
private void addItem() {
movies = new ArrayList<>();
for (int i = 0; i < titleMovie.length; i++){
Movie movie = new Movie();
movie.setTitleMovie(titleMovie[i]);
movie.setDescMovie(descMovie[i]);
movie.setPosterMovie(posterMovie.getResourceId(i,-1));
movie.setGenreMovie(genreMovie[i]);
movie.setDuration(duration[i]);
movie.setDirectorMovie(directorMovie[i]);
movie.setCastMovie(castMovie[i]);
movies.add(movie);
}
adapter.setMovie(movies);
}
}
当我尝试从Android Studio中提供的仿真器运行应用程序时,出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wisnu_1605450.utsmobpro, PID: 18250
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.wisnu_1605450.utsmobpro.MovieAdapter.setMovie(java.util.ArrayList)' on a null object reference
at com.wisnu_1605450.utsmobpro.MovieFragment.addItem(MovieFragment.java:74)
at com.wisnu_1605450.utsmobpro.MovieFragment.onCreate(MovieFragment.java:47)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
非常感谢我为什么搞砸的任何解释,如果上下文需要,还将给出更多代码。
答案 0 :(得分:1)
从片段生命周期documentation开始,onCreate
事件将在onCreateView
事件之前被调用。这意味着当您调用adapter.setMovie(movies)
时,不会创建适配器。它将导致NullPointerException。
您应该在addItem
或onViewCreated
事件中致电onStart
。
答案 1 :(得分:0)
OnCreate()
方法应在onCreateView()
之前调用,然后再调用onViewCreated()
。
而在您的代码中,prepare(),addItem()
方法是在onCreate()
中调用的,因此不会为您的MovieAdapter()
创建实例。请在prepare(),addItem()
中删除onCreate()
方法并将其放置在{{ 1}}