findViewById中NullPointerException的错误
最近在弄一个对话框的登录时,发现一个总是报NullPointerException的错误,折腾了两小时,一直没有发现细小的区别。。先上图,一边说明原因
首先是
Activity类中定义的findViewById()
* There are two methods almost all subclasses of Activity will implement:* * <ul>* <li> {@link #onCreate} is where you initialize your activity. Most* importantly, here you will usually call {@link #setContentView(int)}* with a layout resource defining your UI, and using {@link #findViewById}* to retrieve the widgets in that UI that you need to interact with* programmatically.* * <li> {@link #onPause} is where you deal with the user leaving your* activity. Most importantly, any changes made by the user should at this* point be committed (usually to the* {@link android.content.ContentProvider} holding the data).* </ul>2.private CharSequence mTitle;private int mTitleColor = 0;final FragmentManagerImpl mFragments = new FragmentManagerImpl();final FragmentContainer mContainer = new FragmentContainer() {@Overridepublic View findViewById(int id) {return Activity.this.findViewById(id);}};3./*** Called when the activity is starting. This is where most initialization* should go: calling {@link #setContentView(int)} to inflate the* activity's UI, using {@link #findViewById} to programmatically interact* with widgets in the UI, calling* {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve* cursors for data being displayed, etc.4./*** Finds a view that was identified by the id attribute from the XML that* was processed in {@link #onCreate}.** @return The view if found or null otherwise.*/public View findViewById(int id) {return getWindow().findViewById(id);}
我们调用的findViewById()函数其实有两种(目前我只看到两种,不确定还有没有其他的),一种是Activity类中findViewById()函数
另外一种是View类中定义的findViewById()函数
一般我们在oncreate()方法中使用的(**view)findViewById(R.id.**)既是调用的Activity中的findViewById()函数
而在其他情况写出的***view.findViewById()中调用的是view类中的findViewById()
从这里可以看出这个函数是在寻找在xml中定义的指定id的对象View类中的findViewById()
从这里可以看出我们是从一个view的child view中寻找指定id的对象,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点,或有相同的父亲节点,但不在父节点及以上节点调用findViewById通过id来查找他们就是没有问题。
使用这个函数的常见问题:
仔细看下边两段代码代码
[xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
[xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
一段里边Layout没有id这个参数,一段里边有id,虽然代码不同但在outline中显示出来都是
这样在第一种情况下R.id中可以找到LinearLayout这个控件,第二种是没有的哈,这些也是以后要注意的细节
2.在调用view中的findViewById()一定要想好父View是谁!即**view.findViewById()中的**view要找对,如果没有找对父View,返回基本都是null了
1.在另一个view的元素应该用baseView.findViewById()来拿
findViewById()是要指定view的,如果在该view下找不到,自然报null。平时注意养成写view.findViewById()的习惯就不容易错了。
2.findViewById在setContentView(R.layout.main);之前.
即在setContentView调用之前,调用了findViewById去找main布局中的界面元素lv_contactbook,那么所得到的lv一定是null。正确的做法是将上面代码中加粗的哪一行,挪至setContentView方法调用之后即可。
3.clean一下工程,让ID重新生成
这种情况是调用LayoutInflater.inflate将布局xml规定的内容转化为相应的对象。比如有rowview.xml布局文件(比如在自定义Adapter的时候,用作ListView中的一行的内容的布局),假定在自定的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了。
findViewById中NullPointerException的错误相关推荐
- JAVA Web项目中所出现错误及解决方式合集(不断更新中)
JAVA Web项目中所出现错误及解决方式合集 前言 一.几个或许会用到的软件下载官网 二.Eclipse的[preferences]下没有[sever]选项 三.Tomcat的安装路径找不到 四.T ...
- Sql Server 因为触发器问题导致数据库更新报错“在触发器执行过程中引发了错误,批处理已中止”的问题处理...
在维护一个非常旧的项目时,由于该项目版本已经非常老了,而且在客户现场运行的非常稳定,更要命的是本人目前没有找到该项目的代码,为了处理一个新的需求而且还不能修改程序代码,于是决定从数据库入手,毕竟该项目 ...
- react 错误边界_React with GraphQL和错误边界中的自定义错误页面
react 错误边界 by Abi Noda 通过Abi Noda React with GraphQL和错误边界中的自定义错误页面 (Custom error pages in React with ...
- 诊断IIS中的ASP0115错误
诊断IIS中的ASP0115错误 作者:未知 重要说明:本文包含有关修改注册表的信息.修改注册表之前,一定要备份注册表,并且一定要知道在发生问题时如何还原注册表.有关如何备份.还原和编辑注册表的信息, ...
- php中常见的错误类型有,JavaScript中常见的错误类型有哪些?(详细介绍)
在JavaScript中,当发生错误时会生成描述错误类型的错误对象,此错误对象包含错误类型和编号等信息,这些信息可用于后续处理等,在本篇文章中将给大家介绍常见的错误类型以及如何处理这些错误. Java ...
- windows server 驱动精灵_大神教你如何在Win10中检查磁盘驱动器错误
正文 如果你在使用Win10的过程中遇到随机错误,尤其是与磁盘读取或写入相关的错误,那么你可能需要回想一下,最近有没有不正确地关机,磕碰到硬盘,或者你的硬盘使用了太多的年头. 在这篇文章中,机长将介绍 ...
- GDI+ 中发生一般性错误(生成验证码时出现的错误)
我在生成验证码时,出现这样的错误:GDI+ 中发生一般性错误. 怎么解决? 转载于:https://www.cnblogs.com/happyangle/archive/2008/06/11/1217 ...
- GDI+中发生一般性错误
在PictureBox中有一副图,希望保存成图片,用 pictureBox1.Image("c:\\aa.jpg",System.Drawing.Imaging.ImageForm ...
- python语言包含的错误,Python语言程序中包含的错误,一般分为三种,以下____________不是其中的一种...
Python语言程序中包含的错误,一般分为三种,以下____________不是其中的一种 答:编译错误 人体体温能自动调控在37度,其原因是( ). 答:人体内产生的热能是分批放出的 人体内有完善的 ...
- SharePoint2013 访问“/”应用程序中的服务器错误。解决方案:
SharePoint2013 访问"/"应用程序中的服务器错误.解决方案: 在浏览器中键入访问SharePoint服务器地址时,报如下错误 按照,提示修改web.config文件. ...
最新文章
- Codeforces 847C - Sum of Nestings
- lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为一行...
- SteamVR 工具包VRTK实例解析
- 利用helloworld来检测VPS的对外端口是否打开
- 5g存储服务器是什么项目,5G时代对服务器有什么要求?
- VS2008 JS脚本调试总是调试旧代码 真不知道怎么回事?谁能帮帮我呀!
- Struts2中的值栈
- [转]如何处理机器学习中的不平衡类别
- 域服务器2008系统密码忘记了,如何在Server 2008 R2上重置您忘记的域管理员密码 | MOS86...
- HDU 4380 Farmer Greedy 计算几何+bitset
- 拓端tecdat|R语言惩罚logistic逻辑回归(LASSO,岭回归)高维变量选择的分类模型案例
- HBase: 看上去很美
- 如何将视频生成二维码
- PDF编辑方法,怎么删除PDF文档中的空白页
- PostgreSQL 数据加密之 pgcrypto
- 记录--Spyder打开时出现“An error occurred while starting the kernel“
- 编程的宗派(OOP与FP孰优孰劣)--王垠
- 碎片粘合:Tasking DD 启发的思考
- 锐捷客户端-您不在许可范围中,请确认您的权限
- Bitflip问题案例分析