c# - Xamarin android unable to assign text value to edit text -
i new mobile development. working on android in xamarin using visual studio 2015. following sample code , doing same described in it.
i getting exception when try assign text
value edit text
in update
layout. bellow code in assigning values edit text
void list_itemclick(object sender, adapterview.itemclickeventargs e) { click_employee = e.position + 1; icursor c = dbhelper.getsingleentry(click_employee); c.movetofirst(); name = c.getstring(c.getcolumnindex(dbhelper.employee_name)); email = c.getstring(c.getcolumnindex(dbhelper.employee_email)); phone = c.getstring(c.getcolumnindex(dbhelper.employee_phone)); designation = c.getstring(c.getcolumnindex(dbhelper.employee_designation)); dname.text = name; demail.text = email; dphone.text = phone; ddesignation.text = designation; }
in above code exception raised @ dname.text = name;
bellow error description
system.nullreferenceexception: object reference not set instance of object.
same work performed in show complete data
function there things good, don't know what's happening in update
function
update 1
bellow insert employee
code
namespace appwithdb.resources { [activity(label = "insertemployee")] class insertemployee : activity { edittext name, email, phone, designation; button submitbtn; private sqlitehelper dbhelper; protected override void oncreate(bundle bundle) { base.oncreate(bundle); //create app here setcontentview(resource.layout.insert); initialize(); dbhelper = new sqlitehelper(this); submitbtn.click += submitbtn_click; } private void submitbtn_click(object sender, eventargs e) { if (name.text.tostring().equals("") || email.text.tostring().equals("")|| phone.text.tostring().equals("") || designation.text.tostring().equals("")) { toast.maketext(this, "fields empty found", toastlength.short).show(); } else { dbhelper.insertemployee(name.text.tostring(), email.text.tostring(), phone.text.tostring(), designation.text.tostring()); toast.maketext(this, "data stored successfully", toastlength.short).show(); name.text = " "; email.text = " "; phone.text = " "; designation.text = " "; } } private void initialize() { name = (edittext)findviewbyid(resource.id.empname); email = (edittext)findviewbyid(resource.id.empemail); phone = (edittext)findviewbyid(resource.id.empphone); designation = (edittext)findviewbyid(resource.id.empdesignation); submitbtn = (button)findviewbyid(resource.id.insertempbtn); } }}
update 2
bellow code update employee
namespace appwithdb.resources { [activity(label = "updateemployee")] class updateemployee:activity { listview list; edittext dname, demail, dphone, ddesignation; string name, email, phone, designation; sqlitehelper dbhelper; int click_employee; button upbtn; protected override void oncreate(bundle bundle) { base.oncreate(bundle); // create application here setcontentview(resource.layout.update); initialize(); dbhelper = new sqlitehelper(this); system.collections.arraylist datalist = dbhelper.getallemployeesdata(); string[] myarr = (string[])datalist.toarray(typeof(string)); list.adapter = new arrayadapter(this, android.resource.layout.simpleexpandablelistitem1, myarr); list.itemclick += list_itemclick; upbtn.click += delegate { dbhelper.updateemployeeinfo(click_employee, dname.text.tostring(), demail.text.tostring(), dphone.text.tostring(), ddesignation.text.tostring()); }; } private void initialize() { list = (listview)findviewbyid(resource.id.listview1); dname = (edittext)findviewbyid(resource.id.ename); demail = (edittext)findviewbyid(resource.id.eemail); dphone = (edittext)findviewbyid(resource.id.ephone); ddesignation = (edittext)findviewbyid(resource.id.edesignation); upbtn = (button)findviewbyid(resource.id.updateemploy); } void list_itemclick(object sender, adapterview.itemclickeventargs e) { click_employee = e.position + 1; icursor c = dbhelper.getsingleentry(click_employee); c.movetofirst(); name = c.getstring(c.getcolumnindex(dbhelper.employee_name)); email = c.getstring(c.getcolumnindex(dbhelper.employee_email)); phone = c.getstring(c.getcolumnindex(dbhelper.employee_phone)); designation = c.getstring(c.getcolumnindex(dbhelper.employee_designation)); dname.text = name; demail.text = email; dphone.text = phone; ddesignation.text = designation; } }}
update 3 :
bellow xmls
of complete_data
, update
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="350dp" android:divider="#000" android:dividerheight="1dp" /> <textview android:id="@+id/ename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" /> <textview android:id="@+id/eemail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="email" /> <textview android:id="@+id/ephone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="phone" /> <textview android:id="@+id/edesignation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="designation" /> </linearlayout></scrollview></linearlayout>
update xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="250dp" android:divider="#000" android:dividerheight="1dp" /> <edittext android:id="@+id/upname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="name" /> <edittext android:id="@+id/upemail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="email" /> <edittext android:id="@+id/upphone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="phone" /> <edittext android:id="@+id/updesignation" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="designation" /> <button android:id="@+id/updateemploy" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="update employee" /> </linearlayout></scrollview></linearlayout>
for better understanding sample code given above.
any highly appreciated
the problem use wrong view
ids. in xml there upname
, , searching resource.id.ename
.
fix problem need change resource.id.ename
resource.id.upname
Comments
Post a Comment