The input value inside the android dialog box is not getting, null pointer exception

Source: Internet
Author: User

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 ();

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.