gpt4 book ai didi

java - 尝试在空对象引用上调用虚拟方法 '...(java.util.ArrayList)'

转载 作者:行者123 更新时间:2023-12-02 09:20:49 26 4
gpt4 key购买 nike

我对 Android 开发非常陌生,因为我现在才上了一个类,所以我对当前的情况感到非常困惑,因为我正在使用目前拥有的引用文献编写代码。

我也一直在尝试使用其他来源的引用资料,但遗憾的是,我无法真正理解它们的真正工作原理。

我当前的任务是,我必须制作一个应用程序,作为使用 fragment 的电影和电视节目的目录,以下是其中一个 fragment 的代码:

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)

非常感谢任何关于我搞砸的原因的解释,如果上下文需要的话,还会提供更多代码。

最佳答案

来自 fragment 生命周期documentation , onCreate 事件将在 onCreateView 事件之前调用。这意味着当您调用 adapter.setMovie(movies) 时,不会创建适配器。它会导致 NullPointerException。

您应该在 onViewCreatedonStart 事件中调用 addItem

关于java - 尝试在空对象引用上调用虚拟方法 '...(java.util.ArrayList)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58703674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com