我在尝试更新ViewPager片段中的片段中的ListView时遇到问题。整个场景是一个FragmentActivity像基础一样在片段之间进行通信。具体来说,一个ListFragment单选片段在FragmentActivity中发送对象引用,然后将其重定向到ViewPager片段,后者又应更新该片段的另一个Fragment子代。一切似乎都运行良好,并且第一次创建ViewPager片段会显示我需要的数据,但是,当您在ListFragment中选择一行来启动此过程时,却进入此代码块中的ViewPager片段,public void refreshDataWithClassType(ClassType classType) { Log.d(TAG, "Should refresh data"); FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) viewPager.getAdapter(); for(int i = 0; i < fragmentPagerAdapter.getCount(); i++) { ExamsListFragment viewPagerFragment = (ExamsListFragment) fragmentPagerAdapter.getItem(i); if(viewPagerFragment != null) { // Do something with your Fragment // Check viewPagerFragment.isResumed() if you intend on interacting with any views. viewPagerFragment.refreshExams(classType.getClassTypeId()); } } }我只是将消息发送到每个ViewPagerFragment以刷新我的数据(检查)。但是一段时间后,当我收到NullPointerException时,我发现在refreshExams方法中,public void refreshExams(int classTypeId) { Bundle args = new Bundle(); args.putInt(KEY_POSITION, getArguments().getInt(KEY_POSITION)); args.putInt(KEY_CLASS_TYPE, classTypeId); setArguments(args); getRepositoryExams(); if (examsArrayAdapter == null) Log.d(TAG, "ExamsArrayAdapter is NULL"); if (getActivity() == null) Log.d(TAG, "Activity is NULL"); if (examsList.size() == 0) { examsArrayAdapter.clear(); examsArrayAdapter.notifyDataSetChanged(); } else { examsArrayAdapter = new ExamsArrayAdapter(getActivity(), R.layout.exam_row_layout, (ArrayList<Exam>) examsList); listView.setAdapter(examsArrayAdapter); } }现在,即使我已经应该拥有Fragment的这个实例,也可以在第一次进行良性创建,@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); rootView = inflater.inflate(R.layout.exams_list_fragment, container, false); listView = (ListView) rootView.findViewById(R.id.examsListView); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { callBackExamListener = (OnExamSelectedListener) activity; } catch (ClassCastException ex) { throw new ClassCastException(activity.toString() + " must implement OnExamSelectedListener"); } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getRepositoryExams(); examsArrayAdapter = new ExamsArrayAdapter(getActivity(), R.layout.exam_row_layout, (ArrayList<Exam>) examsList); listView.setAdapter(examsArrayAdapter); listView.setOnItemClickListener(listRowItemClickListener); }getActivity()和examsArrayAdapter为NULL吗?如何解决这个问题?先感谢您! 最佳答案 我实际上在这里识别了一些代码。我认为正在发生的事情是,您没有检查以确保获取的viewPagerFragment位于onResume中,并且如果它们距当前活动页面超过1页,则它们可能会被破坏。如果viewPagerFragment不在onResume中,则需要使用一种方法在viewPagerFragment上设置一个布尔标志,以便当它进入refreshExams时将调用onResume。 FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) viewPager.getAdapter(); for(int i = 0; i < fragmentPagerAdapter.getCount(); i++) { ExamsListFragment viewPagerFragment = (ExamsListFragment) fragmentPagerAdapter.getItem(i); if(viewPagerFragment != null) { // Do something with your Fragment // Check viewPagerFragment.isResumed() if you intend on interacting with any views. if(viewPagerFragment.isResumed()) viewPagerFragment.refreshExams(classType.getClassTypeId()); } }但是,实际上,我发现上述代码仅对我有效,这是因为我创建了FragmentPagerAdapter的方式,因为我使用了ArrayList来保存我的Fragment,它将在。自Android销毁getItem和Activity以来,旋转时此失败。我建议您查询Fragment以使FragmentManager脱离Fragment,并确保在刷新之前检查每个ViewPager是否在viewPagerFragment中,并设置标志以其他方式刷新。对此question的公认答案是浏览您的onResume子级ViewPager的正确方法。 10-05 18:01