位置: 编程技术 - 正文

Android开发:findViewById返回null的解决办法(Android开发工具)

编辑:rootadmin

推荐整理分享Android开发:findViewById返回null的解决办法(Android开发工具),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:Android开发工程师,Android开发工程师,Android开发工具,Android开发工具,Android开发工具,Android开发工具,Android开发是做什么的,Android开发者,内容如对您有帮助,希望把文章链接给更多的朋友!

问题: Android开发:findViewById返回null的解决办法

解决办法:

在用Eclipse进行Android的界面开发,通过findViewById试图获取界面元素对象时,该方法有时候返回null,造成这种情况主要有以下两种情形。

第一种情形是最普通的。比如main.xml如下,其中有一个ListView,其id为lv_contactbook

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android=" android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<EditText android:id="@&#;id/et_search"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=""

/>

<ListView android:id="@&#;id/lv_contactbook"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/></LinearLayout>

如果在Activity对应的代码中,是这样的写的:

@Override

public void onCreate(BundlesavedInstanceState)

{

super.onCreate(savedInstanceState);

ListViewlv = (ListView)findViewById(R.id.lv_contactbook);

setContentView(R.layout.main);

//…

}

即在setContentView调用之前,调用了findViewById去找main布局中的界面元素lv_contactbook,那么所得到的lv一定是null。正确的做法是将上面代码中加粗的哪一行,挪至setContentView方法调用之后。

第二种情形。这种情况下通常是调用LayoutInflater.inflate将布局xml规定的内容转化为相应的对象。比如有rowview.xml布局文件如下(比如在自定义Adapter的时候,用作ListView中的一行的内容的布局):

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android=" android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"> <TextView android:id="@&#;id/tv_contact_id"

android:layout_width="0px"

android:layout_height="0px"

android:visibility="invisible"

android:gravity="center_vertical"

/>

<TextView android:id="@&#;id/tv_contactname"

android:layout_width="wrap_content"

android:layout_height="dip"

android:textSize="dip"

android:layout_marginTop="dip"

android:textColor="#FFFFFFFF"

/>

</LinearLayout>

假定在自定的Adapter的getView方法中有类&#;如下的代码:

View rowview = (View)inflater.inflate(R.layout.rowview, parent, false);

TextView tv_contact_id =(TextView)rowview.findViewById(R.id.tv_contact_id);

TextView tv_contactname =(TextView)rowview.findViewById(R.id.tv_contactname);

有时候居然也会发现rowview非空,但tv_contact_id和tv_contactname都是null!仔细看代码,怎么也看不出错误来。到底是什么原因造成的呢?答案是Eclipse造成的,要解决这个问题,需要这个项目clean一次(Project菜单 -> Clean子菜单),这样就OK了。

第二种情况很隐蔽,因为代码的确没有错。如果一时没有想到解决办法会浪费很多时间。

本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接: version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android=" android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<EditText android:id="@&#;id/et_search"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=""

/>

<ListView android:id="@&#;id/lv_contactbook"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/></LinearLayout>

如果在Activity对应的代码中,是这样的写的:

@Override

public void onCreate(BundlesavedInstanceState)

{

super.onCreate(savedInstanceState);

ListViewlv = (ListView)findViewById(R.id.lv_contactbook);

setContentView(R.layout.main);

//…

Android开发:findViewById返回null的解决办法(Android开发工具)

}

即在setContentView调用之前,调用了findViewById去找main布局中的界面元素lv_contactbook,那么所得到的lv一定是null。正确的做法是将上面代码中加粗的哪一行,挪至setContentView方法调用之后。

第二种情形。这种情况下通常是调用LayoutInflater.inflate将布局xml规定的内容转化为相应的对象。比如有rowview.xml布局文件如下(比如在自定义Adapter的时候,用作ListView中的一行的内容的布局):

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android=" android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"> <TextView android:id="@&#;id/tv_contact_id"

android:layout_width="0px"

android:layout_height="0px"

android:visibility="invisible"

android:gravity="center_vertical"

/>

<TextView android:id="@&#;id/tv_contactname"

android:layout_width="wrap_content"

android:layout_height="dip"

android:textSize="dip"

android:layout_marginTop="dip"

android:textColor="#FFFFFFFF"

/>

</LinearLayout>

假定在自定的Adapter的getView方法中有类&#;如下的代码:

View rowview = (View)inflater.inflate(R.layout.rowview, parent, false);

TextView tv_contact_id =(TextView)rowview.findViewById(R.id.tv_contact_id);

TextView tv_contactname =(TextView)rowview.findViewById(R.id.tv_contactname);

有时候居然也会发现rowview非空,但tv_contact_id和tv_contactname都是null!仔细看代码,怎么也看不出错误来。到底是什么原因造成的呢?答案是Eclipse造成的,要解决这个问题,需要这个项目clean一次(Project菜单 -> Clean子菜单),这样就OK了。

第二种情况很隐蔽,因为代码的确没有错。如果一时没有想到解决办法会浪费很多时间。

本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接: version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android=" android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<EditText android:id="@&#;id/et_search"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=""

/>

<ListView android:id="@&#;id/lv_contactbook"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/></LinearLayout>

如果在Activity对应的代码中,是这样的写的:

@Override

public void onCreate(BundlesavedInstanceState)

{

super.onCreate(savedInstanceState);

ListViewlv = (ListView)findViewById(R.id.lv_contactbook);

setContentView(R.layout.main);

//…

}

即在setContentView调用之前,调用了findViewById去找main布局中的界面元素lv_contactbook,那么所得到的lv一定是null。正确的做法是将上面代码中加粗的哪一行,挪至setContentView方法调用之后。

第二种情形。这种情况下通常是调用LayoutInflater.inflate将布局xml规定的内容转化为相应的对象。比如有rowview.xml布局文件如下(比如在自定义Adapter的时候,用作ListView中的一行的内容的布局):

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout

xmlns:android=" android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"> <TextView android:id="@&#;id/tv_contact_id"

android:layout_width="0px"

android:layout_height="0px"

android:visibility="invisible"

android:gravity="center_vertical"

/>

<TextView android:id="@&#;id/tv_contactname"

android:layout_width="wrap_content"

android:layout_height="dip"

android:textSize="dip"

android:layout_marginTop="dip"

android:textColor="#FFFFFFFF"

/>

</LinearLayout>

假定在自定的Adapter的getView方法中有类&#;如下的代码:

View rowview = (View)inflater.inflate(R.layout.rowview, parent, false);

TextView tv_contact_id =(TextView)rowview.findViewById(R.id.tv_contact_id);

TextView tv_contactname =(TextView)rowview.findViewById(R.id.tv_contactname);

有时候居然也会发现rowview非空,但tv_contact_id和tv_contactname都是null!仔细看代码,怎么也看不出错误来。到底是什么原因造成的呢?答案是Eclipse造成的,要解决这个问题,需要这个项目clean一次(Project菜单 -> Clean子菜单),这样就OK了。

第二种情况很隐蔽,因为代码的确没有错。如果一时没有想到解决办法会浪费很多时间。

本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:

.隐藏应用名称,全屏显示应用? 问题:隐藏应用名称,全屏显示应用?解决办法:在项目清单文件manifest.xml中activityandroid:theme=@android:style/Theme.NoTitleBar.Fullscreen/activity版权声明:本文为博

安卓 网络图片查看器 设计思路:输入网络图片的地址,点击浏览按钮可以显示网络中的图片。运用线程来实现。1.子线程利用handle来发送消息,消息被放在主线程中,looper消

AIDL:远程调用服务里的方法失败,提示取得的IBinder对象NullPointerException 问题:AIDL:远程调用服务里的方法失败,提示取得的IBinder对象NullPointerException解决办法:不要忘记了在Service中的onBind()方法中返回IBinder对象.returnnewMyIBinder;版

标签: Android开发工具

本文链接地址:https://www.jiuchutong.com/biancheng/381013.html 转载请保留说明!

上一篇:[安卓]手机管家(十八)一键 锁屏清理线程以及widget(安卓手机管家怎么关闭)

下一篇:16.隐藏应用名称,全屏显示应用?(隐藏应用名字)

  • 暂估入库收到发票后怎么做分录
  • 红字发票的数电票号码是啥
  • 发票勾选平台怎么导出未勾选的发票
  • 构成业务和不构成业务的会计处理
  • 其他综合收益税后净额影响净利润吗
  • 用友软件作废凭证
  • 地税退税政策
  • 日用品增值税税率
  • 移动板房的税法是多少
  • 授权费计入什么科目
  • 所得税申报季末资产总额怎么填
  • 1697510614
  • 发放给职工
  • vue获取当前页面请求头信息
  • 电子承兑汇票贴现怎么做账
  • 收据大写后面的数字
  • 腾讯电脑管家中蓝牙在哪
  • 顺丰充值的钱可以退吗
  • 苹果手机把旧手机的照片传到新手机
  • php语言标记风格有四种,分别是
  • 交通费补贴算工资吗?
  • 企业间合作建房且建成后自用
  • 财务专用章是干啥的
  • 世界著名气泡酒公司
  • 收到职称评审费怎么做账
  • php返回数据给ajax
  • php使用( )关键字来创建对象
  • 框架 frame
  • 补税分录
  • python字典遍历key
  • 个税申报和社保申报的金额要一致吗
  • 事业单位回复短信里的加号要吗
  • 利息补缴税款加收利息计算
  • 交易性金融资产公允价值变动计入
  • 费用报销单怎么填写
  • 工资发放凭证原件指什么意思
  • 民办非企业单位工会经费
  • 列入固定资产的标准
  • 寄售商店代销寄售物品的税率
  • 建筑业清包工合同范本
  • 住宿费发票可以报销吗
  • 工程结算收入是否缴纳增值税
  • 管理费用怎样分摊归集到产品
  • 外币投资计入什么科目
  • 合同负债属于什么账户
  • 施工单位的罚款标准
  • 公司的软件服务器设置什么意思啊
  • 支付兼职人员工资会计处理
  • 新成立的公司工会经费免交一年吗
  • 编制合并财务报表的依据是纳入合并
  • mysql查询语句菜鸟教程
  • ipv6文件
  • cndll.dll
  • mac移动硬盘怎么改名
  • centos7 nis
  • windows server 2016正式版下载激活安装设置教程
  • linux安装vmvare
  • centos 6.5安装教程
  • icore是什么意思
  • Ghost XP SP3 YN8.0装机版 (雨林木风)
  • linux块设备读写和回写
  • win8.1卸载软件在哪里
  • win7系统通过wmic命令
  • win7怎么安装网络驱动程序
  • shader cull
  • ExtJs3.0中Store添加 baseParams 的Bug
  • pycharm远程调试代码
  • js document.cookie
  • 怎么查看u盘有没有传输过数据
  • javascript definitive guide
  • javascript中this的用法
  • js如何使用
  • JavaScript性能优化
  • 税务局周末上班没
  • 南京电子税务局咨询电话
  • 北京病退流程
  • 消费税的税收优惠有哪些
  • 广东省地方税务局电子办税服务厅
  • 西安市个税证明去哪里打印
  • 2023个税税率及速算扣除数
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设