findViewById中NullPointerException的错误

来源:互联网 发布:oracle数据库最高权限 编辑:程序博客网 时间:2024/06/05 04:37

最近在弄一个对话框的登录时,发现一个总是报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() {        @Override        public 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了。

0 0
原创粉丝点击