In Android, how does findViewById () h obtain the NULL pointer of EditText?

Source: Internet
Author: User

When I create another program today, I find that when I use findViewById (R. id. edit) to retrieve EditText, I always report a null pointer error. I think it is impossible !!
Finally, starting from findViewById (), we can find that the R. id. edit in this method is obtained from the main layout file xml of the current Activity or Dialog.
For example, my program:
In the ListActivity class:
.......
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. list_view );
}
.....
/**
* The pop-up input window is displayed.
**/
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); // obtain the input text box if it is changed to EditText editText = (EditText) this. findViewById (R. id. input_content); // Null Pointer Error
New AlertDialog. Builder (this)
. SetTitle ("RENAME File" + new File (fileBean. getPath (). getName ())
. SetView (layout)
. SetPositiveButton ("OK", new MyDialogListener (editText ))
. SetNegativeButton ("cancel", new MyDialogListener (editText). show ();
}
Layout file:
Input_dialog.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Pop up the layout file in the input dialog box -->
<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"?>
<! -- File display list layout -->
<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>
Cause: Because EditText editText = (EditText) layout. findViewById (R. id. input_content); is to find the child element whose ID is input_content in layout of the Dialog box
EditText editText = (EditText) this. findViewById (R. id. input_content); // NULL pointer error because the EditText sub-element with the ID of input_content is searched from the this object, that is, the current ListActivity layout, List_view.xml, the List_view.xml layout file does not define this element, so the NULL pointer is incorrect, and our purpose is not like this ....
 
You just need to understand findViewById .....

From Wei Yirong's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.