在Android中旋转屏幕后,ViewPager findViewById为null [英] ViewPager findViewById is null after screen rotation in android

查看:122
本文介绍了在Android中旋转屏幕后,ViewPager findViewById为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 viewPager 来显示一些表格.我的问题是,当我旋转设备的屏幕并再次创建 viewPager 时,当他到达 findViewById viewPager >,始终为 null .我不知道轮换时如何处理.我尝试了 onConfigurationChange ,但是它不起作用,我试图保存寻呼机的 Id ,然后使用保存的 id 进行检索,但是没什么所以我现在很受阻.

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);

在onViewCreated

In onViewCreated

推荐答案

片段

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

这篇关于在Android中旋转屏幕后,ViewPager findViewById为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆