#Remember a fragment using a null pointer error NullPointerException

 最近  研究研究一个  主界面  里面用到了fragment 嵌套  结果  出现了最让人头疼的东西    NullPointerException - 空指针引用异常   其实并不是什么难题  但是很容易出错  今天贴出来  给大家分享一下  希望对大家有帮助

 问题是这样出现的

1. Referencing this fragment in the activity works fine before

      @SuppressLint("NewApi")
    private void initialize() {
        cycleViewPager = (CycleViewPager) getFragmentManager()          .findFragmentById(R.id.fragment_cycle_viewpager_content);
 }

2. After that, I made a big mistake and glued the code directly because I abandoned this activity and wanted to nest it on the new fragment. . . . Then I searched for various null pointers for a long time and finally solved it. Let me explain it here.

3. Because it is different to build a fragment in an activity and nest a fragment in a fragment, and then you need to change the method getChildFragmentManager() when nesting

  cycleViewPager = (CycleViewPager) getChildFragmentManager().findFragmentById(R.id.fragment_cycle_viewpager_content);

It means that the problem is solved by getting the sub-fragment.

Although it is not a big problem, it is easy to overlook it. Thank you!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479287&siteId=291194637