位置: 编程技术 - 正文

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

  • 税款所属期错了怎么更正
  • 应交增值税账面和申报表不符
  • 电子发票冲红后算金额吗?
  • 企业名称变更需要重新核名吗
  • 因保管不善,不慎遗失
  • 以固定资产换入原材料
  • 住房公积金部分业务暂停办理
  • 提取公积金收费比例
  • 物业公司支付出的费用
  • 工程款转账一般要多久
  • 出售设备账务处理
  • 哪些发票可以报销抵税
  • 通讯费企业所得税计算
  • 没有取得发票可以抵扣成本吗
  • 小规模纳税人公司注销流程及费用
  • 以公允价值模式后续计量的投资性房地产无须计提减值
  • 机动车发票怎么作废
  • 违约支付罚款计入哪里
  • 实收资本不是股东打来的怎么调帐
  • 出口退税两单两票
  • 技术服务收入和产品服务收入举个例子
  • 对方开的销项负数抵扣联怎么处理
  • 税率抵扣计算公式
  • 预付业务招待费会计分录
  • services. exe
  • 承租集体土地如何确权
  • 印花税会计分录2023
  • 被公司辞退有钱吗
  • 印花税贴花怎么贴划线
  • 职工福利费用怎么入账
  • 纳税人数字签名怎么填
  • css设置3d
  • paper 1
  • xa 事务
  • 帝国cms使用手册
  • 亏损合同预计负债的会计分录
  • 汽车4s店,厂家返修
  • 如何确定可以结婚生子
  • 房地产返佣
  • 差旅费抵扣进项税额
  • 房产税计入管理费用还是税金附加
  • 以货换货账务怎么处理
  • 百旺金赋抄报税指南
  • 低值易耗品如何界定
  • 如何区分生产类型
  • 个人独资企业费用扣除
  • 外币折算会计思维导图
  • 购买固定资产的增值税计入成本吗
  • 个税起征点调整最新消息
  • 复利现值系数表怎么算
  • 怎样计算税款
  • 机票行程单如何看座位等级
  • 开餐饮店需要什么条件才能开
  • sql数据库连不上可能的原因
  • windows的实验步骤
  • win10屏幕自动变黄
  • txt无法打开怎么弄
  • win8.1系统安装教程
  • xp桌面右键选项消失
  • 苹果电脑mac设备怎么删除
  • 系统配置运行命令
  • win8休眠如何唤醒
  • node linux安装
  • 粒子冲突
  • unity3d 碰撞
  • node.js net模块
  • Android Chromium WebView学习启动篇
  • ios shell脚本
  • 深入理解python异步编程
  • jquery如何实现双向绑定
  • linux shell脚本教程
  • unity的ugui
  • 计算字符串的长度使用哪个函数?
  • unity shooter
  • 税务核销
  • 税务管理职责
  • 湖南耕地占用税标准
  • 电子税务网没开通怎么办
  • 中国税务网官网1732171695993732.2418.61431871
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设