位置: 编程技术 - 正文

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

  • 个税应纳税所得额计算公式
  • 高档珍珠镶嵌
  • 出售无形资产属于资产处置损益吗
  • 农机合作社项目复核报告
  • 加计抵扣进项税进营业外收入
  • 物流托运不给发货怎么办
  • 资产总计是期初余额吗
  • 开票地址开错有什么后果
  • 财产转让所得的纳税义务发生时间
  • 收到前欠销货款290000元,存入银行的会计分录怎么写
  • 承兑差额怎么做账
  • 12月份费用可以计提吗
  • 第三方投资入股
  • 计提个人生产经营所得个税需要贴附件吗
  • 小规模纳税人开专票税率是1%还是3%
  • 不动产抵押登记费记什么科目
  • 带息票据贴现如何计算
  • linux系统的文件与目录操作
  • php 递归函数
  • php curl模块
  • php
  • 个人销售住房是否免征土地增值税
  • wordpress优化seo
  • 设备维修的会计分录怎么做
  • 对公账户转入对私账户
  • 真菌感染手指甲空了
  • 详解Yii2.0 rules验证规则集合
  • 公司买发票的费用怎么做账?
  • 微信小程序网页版
  • 强大的图片预览软件
  • html代码form
  • js轮播图视频教程
  • 印花税计提比例是多少
  • 哪些农产品按照鲜品统计
  • 购入苗木进项税的会计分录
  • 预算会计的核算对象是什么
  • dedecms默认用户名
  • 首涂24套
  • 规模以上企业纳税要求
  • 免税农产品发票怎么做账
  • sql server 2005如何使用
  • 开了发票不做收入的账务处理是?
  • 企业的项目有哪些
  • 会计核算职能有全面性吗
  • 经营项目里没有纹身可以纹身吗
  • 融资性售后回租承租方出售资产为什么不缴纳增值税
  • 行政单位拨出经费的规定
  • 政府扶助资金
  • 应交销项税转出分录
  • 建筑业预缴税款怎么退税
  • 财务费用贷方余额怎么结转本年利润
  • 视同销售和不视同销售的区别?
  • 小规模纳税人开专票需要交税吗
  • 为什么到期一次还本付息要用债权投资利息调整
  • 出租房屋的广告怎么写好
  • mssqlserveradhelper
  • 微软2016是window多少
  • win8装不了itunes
  • winxp系统怎么投屏
  • ubuntu git not found
  • 如何将windows文件复制到ubuntu
  • 影响电脑速度的因素
  • macos catalin
  • gacrunner.exe是什么
  • 如何提升windows版本
  • win7怎么删除右键菜单
  • win8系统修复在哪里
  • openvz
  • 使用NGUI时遇到物理引擎错误
  • Python IDE PyCharm的基本快捷键和配置简介
  • 浅谈自己对教育的理解
  • python的特点及应用范围
  • Python高手之路第3版PDF下载
  • listview控件设置多个列
  • shell for遍历
  • jquery滚动条滚动到指定位置
  • javascript获取数据
  • 从香港回内地需要做核酸检测吗
  • 广东可以去吗
  • 无锡国税局电话咨询热线
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设