位置: 编程技术 - 正文

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

  • 增值税普通发票几个点
  • 什么是红字增值服务
  • 购房发票契税票丢了可以补吗
  • 提供劳务应收未收的款项
  • 业务提成模版
  • 核定征收需要什么条件
  • 小规模纳税人免征增值税政策
  • 以前年度损益调整
  • 税务局代增值税专用发票冲红,如何重新申报退税
  • 收到社保中心的生育经贴怎么做账
  • 单位月工资总额
  • 三废一览表
  • 公司不给发公司
  • 企业进项税和销项税抵扣政策
  • 个人所得税的速算扣除数是什么意思
  • 三证合一后未办理税务登记
  • 增值税专用发票的税率是多少啊
  • 电梯维修公司发展前景
  • 工会经费的开支必须取得发票么
  • 坏账减值准备账务处理
  • 员工 意外保险
  • 即征即退申报表如何填写
  • 保总保安服务有限公司
  • PHP:pg_send_query()的用法_PostgreSQL函数
  • php获取useragent
  • 无形资产研发成功后的支出
  • 桑吉尔夫个人简介
  • LIO-SAM学习与运行测试数据集
  • 企业职工福利费包括哪些内容
  • ros算法
  • yolov5实例分割原理详解
  • javaweb界面设计
  • less变量
  • 21世纪20年代的中国
  • 10年未被强制修复!黑客利用Windows旧漏洞攻击通信公司并分发恶意文件
  • 纳税检查调整销售额什么意思
  • python 元类 详解
  • 发票没有填写开户行,可以报销吗
  • 一般纳税人不动产租赁可以简易征收吗
  • 邮寄的发票对方没收到怎么办
  • 织梦模板首页logo修改
  • 周转材料属于什么
  • 汽车4s店售后业绩看板
  • 混合销售行为的例子
  • 普通发票能不能重新开
  • 资产合计是期末余额吗
  • sql server游标
  • 银行利息怎么算10万块三年多少利息
  • 基础电信服务和增值电信服务税率
  • sqlserver 数据迁移
  • 公司 用车
  • 发票税额不全能抵扣吗
  • 在建工程的消防要求
  • 技术开发技术服务属于什么行业
  • 负数发票怎么开具?
  • 年末结账与财务的关系
  • 固定资产公司
  • 应收帐款坏账处理
  • sql server数据库数据备份
  • sql语句 时间
  • mysql跨服务器查询语句
  • win7历史记录在哪里
  • 苹果15手机价格和图片颜色
  • freebsd查看路由
  • centos安装位置选择
  • win8怎么一开机就进入桌面
  • 64位win7安装kb3038314补丁更新失败临时解决方法
  • cocos2dx4.0教程
  • 微信小程序实现微信支付
  • 如何短时间内学会打字
  • [置顶] 混合、反走样、雾效、多边形偏移
  • 图片批量压缩到200k以下
  • 深入浅出javascript
  • unity3D利用W,A,S,D让物体移动
  • js日历控件代码和效果
  • python脚本怎么编写
  • junit 原理
  • jquery使用教程
  • python标准库参考手册
  • 东莞市官网
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设