Write an android dialog box, click the button to get edittext inside the value, here has been reported null pointer exception, studied for a long time finally solved.
Exceptions are as follows:
My original code:
The update dialog box private void Updatedialog (final String id) {tablelayout updatemsg = (tablelayout) getlayoutinflater (). Inflate ( R.layout.updatemsg, NULL), new Alertdialog.builder (this). Settitle ("Update this Message"). Setview (updatemsg). Setpositivebutton ( "Update", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method Stubstring MUSICN = ((EditText) Findviewbyid (R.ID.MUSICN)). GetText (). toString (); String Singern = ((EditText) Findviewbyid (R.id.singern)). GetText (). toString (); UpdateData (Db,id,musicn,singern);}}). Setnegativebutton ("Cancel", new Onclicklistener () {public void OnClick (Dialoginterface dialog,int which) {}}). Create (). Show ();}
Another XML configuration file Updatemsg.xml is introduced in the dialog box:
<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <TableRow> &L T TextView android:layout_width= "100dip" android:layout_height= "Wrap_content" android:text= "Music name" android:textsize= "30px" android:textcolor= "#ffffff"/> <edittext Andro Id:id= "@+id/musicn" android:layout_width= "100dip" android:layout_height= "Wrap_content"/> < /tablerow> <TableRow> <textview android:layout_width= "100dip" Android:layout_h eight= "Wrap_content" android:text= "singer" android:textsize= "30px" android:textcolor= "#ffffff "/> <edittext android:id=" @+id/singern "android:layout_width=" 100dip "Andro id:layout_height= "Wrap_content"/&Gt </tablerow></tablelayout>,
Problem solving
The reason for the error is that the XML introduced
When looking for edittext through Findviewbyid, it is found from the original layout file, not the layout file of the dialog box, so of course to report null pointer exception
How can I find a layout file for a dialog box?
Change those two sentences into;
String MUSICN = ((EditText)updatemsg. Findviewbyid (R.ID.MUSICN)). GetText (). toString ();
String Singern = ((EditText) Updatemsg.findviewbyid (R.id.singern)). GetText (). toString ();