Android中findViewById()h擷取EditText null 指標問題

來源:互聯網
上載者:User

今天再做一個程式時,發現我使用findViewById(R.id.edit)擷取EditText時總是報null 指標錯誤,我想不可能啊!!
最後從findViewById()下手,才發現原來此方法中的R.id.edit是從當前Activity或者Dialog的主布局檔案xml中擷取。
比如:我的程式:
ListActivity類中:
。。。。。。。
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list_view);
}
。。。。。
/**
  * 顯示彈出的輸入視窗
  * */
 public void showInputDialog(FileBean fileBean) {
  LayoutInflater layoutInflater = getLayoutInflater();
  View layout = layoutInflater.inflate(R.layout.input_dialog,
    (ViewGroup) findViewById(R.id.input_dialog));
  EditText editText = (EditText)layout.findViewById(R.id.input_content);// 擷取輸入文字框  如果改成EditText editText = (EditText)this.findViewById(R.id.input_content);// null 指標錯誤
  new AlertDialog.Builder(this)
    .setTitle("重新命名檔案" + new File(fileBean.getPath()).getName())
    .setView(layout)
    .setPositiveButton("確定", new MyDialogListener(editText))
    .setNegativeButton("取消", new MyDialogListener(editText)).show();
 }
布局檔案:
input_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 彈出輸入對話方塊布局檔案 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:orientation="horizontal" android:id="@+id/input_dialog">
 <TextView android:id="@+id/input_hint" android:layout_width="60dip"
  android:layout_height="50dip" android:textSize="15dip" android:text="@string/rename_file"></TextView>
 <EditText android:id="@+id/input_content" android:minWidth="120dip"
  android:layout_width="150dip" android:layout_height="50dip"></EditText>
</LinearLayout>
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 檔案顯示列表布局 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:orientation="vertical">
 <TextView android:id="@+id/title" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="@string/title">
 </TextView>
 <LinearLayout android:id="@+id/s_layout"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
  <EditText android:id="@+id/t_search" android:layout_width="280dip"
   android:layout_height="40dip" android:text="@string/search"
   android:singleLine="true"></EditText>
  <ImageButton android:id="@+id/b_search"
   android:layout_width="40dip" android:layout_height="40dip"
   android:background="@drawable/search"></ImageButton>
 </LinearLayout>
 <TextView android:id="@+id/cur_dir" android:layout_width="wrap_content"
  android:layout_height="wrap_content"></TextView>
 <ListView android:id="@id/android:list" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:scrollbarSize="15dip"
  android:fadingEdge="none" android:divider="#222553"
  android:dividerHeight="2dip" android:cacheColorHint="#000000"></ListView>
</LinearLayout>
原因:因為EditText editText = (EditText)layout.findViewById(R.id.input_content);是從Dialog對話方塊布局layout中尋找ID為input_content的子項目
   EditText editText = (EditText)this.findViewById(R.id.input_content);// null 指標錯誤    因為是從this對象即當前ListActivity的布局List_view.xml中尋找ID為input_content的子項目EditText,而我們在List_view.xml布局檔案中並無定義此元素故null 指標錯誤 ,並且我們的目的並不是這樣。。。。
 
只要理解findViewById就行了。。。。。 

摘自  魏藝榮的專欄 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.