位置: 编程技术 - 正文

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.隐藏应用名称,全屏显示应用?(隐藏应用名字)

  • 公共电话亭是否应该被拆除
  • 应付职工薪酬费用的对应科目
  • 外来原始凭证包括哪些入库单
  • 核定征收可以不开免税普票吗
  • 建筑业企业生产经营情况表
  • 建筑施工企业涉税问题
  • 残保金属于税种吗
  • 销售货物没有开票如何处理
  • 外币账户间互转流程
  • 到账的钱还能退回去吗
  • 什么叫单项计提坏账损失
  • 扶持资金属于政府补助吗
  • 停工损失会计科目
  • 财会【2016】22号文
  • 集团内部企业之间借款利息增值税
  • 票据结算包括哪几项
  • 决算帐表不相符怎么处理
  • 外贸公司有出口退税吗
  • 子网掩码和默认网关怎么填
  • 未形成固定资产的项目卖出怎么入账
  • 什么车不用交保险
  • 表彰比例如何确定
  • linux root没有权限
  • 佣金代扣代缴增值税需要缴纳附加税吗
  • 共管账户的定义
  • 货款已预付会计分录
  • 增值税跟企业所得税的关系
  • 设计资质承担范围
  • Smarty3配置及入门语法
  • 实收资本印花税税率多少
  • ai绘画图片
  • 无法将node项识别为
  • jasypt加密解密
  • 每个开发人员都有编制吗
  • pytorch的环境配置
  • sdiff命令 以并排方式合并文件之间的差异
  • 命名空间 php
  • 股权收购账务处理
  • 网上学电脑的软件
  • 非本公司员工能上班吗
  • 新开办公司如何办理金税盘
  • 建筑企业增值税税率是多少
  • 如何查看简易征信报告
  • mysqli
  • 什么人适合单干
  • 企业累计预扣个税是什么
  • 技术服务合同的税率
  • mongodb 教程
  • 实收资本属于限定性净资产吗
  • 会计账簿登记错误
  • 销售商品开票税目
  • 个体户超额
  • 简易征收的项目进项税可以抵扣吗
  • 已认证专票发现地址为错误
  • 售后租回会计处理分录
  • 对方开普票,怎么扣税
  • 不需要缴纳税款的企业
  • 营业外收入主要来源
  • 在你登陆时发生了问题
  • 分公司可以设立公司吗
  • 银行日记账年底是0第二年年初怎么写
  • 我的发票查询
  • mysql用处
  • mac复制文件路径后怎么粘贴
  • 苹果系统装win8
  • windows8远程桌面连接
  • xp无法进入桌面怎么办
  • linux配置java环境变量无法保存并退出
  • centos下载软件并安装
  • Win10 64位系统下火狐浏览器打开带flash网页卡死的解决方法
  • javascript+
  • javascript事件模型介绍
  • jQuery插件封装时如要实现链式编程,需要
  • 网页设计css文字居中
  • node javascript
  • unity引擎工具
  • jquery怎么写轮播图
  • 银行扣账户维护费会计分录
  • 浙江网上税务局申报
  • 中国税务局发票
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设