没有合适的资源?快使用搜索试试~ 我知道了~
Android中findViewById返回为空null的快速解决办法
1 下载量 162 浏览量
2021-01-20
09:46:47
上传
评论
收藏 46KB PDF 举报
温馨提示
试读
2页
【问题描述】 Android中如下代码: LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout.fragment_field_list); 返回为null。 【解决过程】 1.参考: android – getActivity().findViewById(R.layout.contacts_list_view) returns null – Stack Overflow AndroidGUI27中findViewById返回null的快速解决办法 – 玄机逸士的专栏 – 博客频道 – CSDN.NET
资源详情
资源评论
资源推荐
Android中中findViewById返回为空返回为空null的快速解决办法的快速解决办法
【问题描述】【问题描述】
Android中如下代码:
LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout.fragment_field_list);
返回为null。
【解决过程】【解决过程】
1.参考:参考:
android – getActivity().findViewById(R.layout.contacts_list_view) returns null – Stack Overflow
AndroidGUI27中findViewById返回null的快速解决办法 – 玄机逸士的专栏 – 博客频道 – CSDN.NET
但是没搞定。
2.后来是去搜:后来是去搜:
findViewById R.layout null
而最终找到并参考:
[Android]inflate方法与 findViewById 方法区别 | LayoutInflater的inflate函数用法详解 – loyea – 博客园
去换为:
LayoutInflater inflater = (LayoutInflater)tabContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout groupPollingAddress = (LinearLayout)inflater.inflate(R.layout.fragment_field_list, null);
即可。
3.另外的,类似的:另外的,类似的:
TextView tab1AllGroupPollingAddressLabel = (TextView) findViewById(R.id.lblVariable);
也是返回null,所以去换为:
TextView tab1AllGroupPollingAddressLabel = (TextView) groupPollingAddress.findViewById(R.id.lblVariable);
即可。
【总结】【总结】
此处findViewById返回为null,原因是:
没有在索要find的子view的Parent中去找
或者是:
当然的View下面,没有包含对应的想要找的view,
从而导致找不到,返回null。
解决办法是:解决办法是:
找到要找的view的parent或root的view
再在父级的view中找你要的子view就可以了。
常见的写法是:常见的写法是:
LayoutInflater inflater = (LayoutInflater)tabContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout groupPollingAddress = (LinearLayout)inflater.inflate(R.layout.fragment_field_list, null);
其中是先去找到当前系统的Layout,然后实例化,然后在全局的view中再去找你的view就可以找到了。
PS::Android 自定义自定义view中中findViewById为空的快速解决办法为空的快速解决办法
weixin_38633475
- 粉丝: 3
- 资源: 946
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0