ViewPager findViewById is null after screen rotation in android

风格不统一 提交于 2021-02-08 10:17:30

问题


I'm using a viewPager to display some forms. My problem is when i rotate the screen of the device and the viewPager is created again, it doesn't see the viewPager when he reaches the findViewById, it's always null. I have no idea how to handle it when rotation change. I tried the onConfigurationChange but it doesn't work, I tried to save the Id of the pager and then retrieve it using that saved id but nothing too. So I'm quite blocked now.

pager = (CustomViewPager) getActivity().findViewById(R.id.pager);
pager.setPagingEnabled(false);

In onViewCreated


回答1:


do this in onActivityCreate of the fragment

pager = (CustomViewPager) getActivity().findViewById(R.id.pager);
pager.setPagingEnabled(false);


来源:https://stackoverflow.com/questions/21699006/viewpager-findviewbyid-is-null-after-screen-rotation-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!