android的findviewbyid,【已解决】Android中findViewById返回为空null

【问题】

Android中如下代码:LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout.fragment_field_list);

返回为null。

【解决过程】

1.参考:

但是没搞定。

2.后来是去搜:

findViewById R.layout null

而最终找到并参考:

去换为: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就可以找到了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值