AndroidGUI27中findViewById返回null的快速解決辦法
在用Eclipse進行Android的界面開發(fā),通過findViewById試圖獲取界面元素對象時,該方法有時候返回null,造成這種情況主要有以下兩種情形。
第一種情形是最普通的。
比如main.xml如下,其中有一個ListView,其id為lv_contactbook
<?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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調(diào)用之前,調(diào)用了findViewById去找main布局中的界面元素lv_contactbook,那么所得到的lv一定是null。正確的做法是將上面代碼中加粗的哪一行,挪至setContentView方法調(diào)用之后。
第二種情形。
這種情況下通常是調(diào)用LayoutInflater.inflate將布局xml規(guī)定的內(nèi)容轉化為相應的對象。比如有rowview.xml布局文件如下(比如在自定義Adapter的時候,用作ListView中的一行的內(nèi)容的布局):
<?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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="36dip" android:textSize="16dip" android:layout_marginTop="10dip" 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);
有時候居然也會發(fā)現(xiàn)rowview非空,但tv_contact_id和tv_contactname都是null!仔細看代碼,怎么也看不出錯誤來。到底是什么原因造成的呢?答案是Eclipse造成的,要解決這個問題,需要這個項目clean一次(Project菜單 -> Clean子菜單),這樣就OK了。
第二種情況很隱蔽,因為代碼的確沒有錯。如果一時沒有想到解決辦法會浪費很多時間。
以上所述是小編給大家介紹的AndroidGUI27中findViewById返回null的快速解決辦法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
OpenHarmony如何調(diào)用電話服務API撥打電話
OpenHarmony3.1版本標準系統(tǒng)增加了通話相關的聯(lián)系人應用,來電應用等,在系統(tǒng)服務層面電話相關功能也比較完善,這篇文章主要介紹了OpenHarmony如何調(diào)用電話服務API撥打電話2022-11-11關于AndroidStudio R文件莫名其妙缺失的快速解決方法
下面小編就為大家?guī)硪黄P于AndroidStudio R文件莫名其妙缺失的快速解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android中TabLayout+ViewPager 簡單實現(xiàn)app底部Tab導航欄
TabLayout 是Android com.android.support:design庫的一個控件。本文主要給大家介紹TabLayout+ViewPager 簡單實現(xiàn)app底部Tab布局,需要的的朋友參考下2017-02-02Flutter?fluro時報錯type?'String'?is?not?a?subty
這篇文章主要介紹了Flutter使用fluro時報錯type?'String'?is?not?a?subtype?of?type?'Queue<Task>'解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-12-12Android?NotificationListenerService?通知服務原理解析
這篇文章主要為大家介紹了Android?NotificationListenerService?通知服務原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Android使用Handler實現(xiàn)下載文件功能
這篇文章主要為大家詳細介紹了Android使用Handler實現(xiàn)下載文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06